Skip to content

Commit

Permalink
improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zxbodya committed Jul 28, 2023
1 parent 10e1cc5 commit a9a01b0
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/docs/docs/usage/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ import ajvRuntimeUcs2length from 'ajv/dist/runtime/ucs2length';
import ajvRuntimeUri from 'ajv/dist/runtime/uri';
import * as ajvFormats from 'ajv-formats/dist/formats';

// dependencies to replace in generated code, to be provided by at runtime
const validatorsBundleReplacements: Record<string, [string, unknown]> = {
// '<code to be replaced>': ['<variable name to use as replacement>', <runtime dependency>],
'require("ajv/dist/runtime/equal").default': ['ajvRuntimeEqual', ajvRuntimeEqual],
'require("ajv/dist/runtime/parseJson").parseJson': ['ajvRuntimeparseJson', ajvRuntimeparseJson],
'require("ajv/dist/runtime/parseJson").parseJsonNumber': [
Expand Down Expand Up @@ -179,7 +181,13 @@ if (typeof window !== 'undefined') {
};
}

export function loadSchema(id: string, code: string, nonce: string) {
/**
* Evaluate precompiled validator in browser using script tag
* @param id Identifier to avoid evaluating the same code multiple times
* @param code Code generated server side using `compileSchemaValidatorsCode`
* @param nonce nonce attribute to be added to script tag (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#using_nonce_to_allowlist_a_script_element)
*/
export function evaluateValidator(id: string, code: string, nonce: string): Promise<ValidatorFunctions> {
let maybeValidator = schemas.get(id);
if (maybeValidator) return maybeValidator.promise;
let resolveValidator: (result: ValidatorFunctions) => void;
Expand Down Expand Up @@ -207,13 +215,10 @@ From React component this can be used as following:
```tsx
let [precompiledValidator, setPrecompiledValidator] = React.useState<ValidatorFunctions>();
React.useEffect(() => {
loadSchema(
// some schema id to avoid evaluating it multiple times
schemaId,
// result of compileSchemaValidatorsCode returned from the server
code,
// nonce script tag attribute to allow this ib content security policy for the page
nonce
evaluateValidator(
schemaId, // some schema id to avoid evaluating it multiple times
code, // result of compileSchemaValidatorsCode returned from the server
nonce // nonce script tag attribute to allow this ib content security policy for the page
).then(setPrecompiledValidator);
}, [entityType.id]);

Expand Down

0 comments on commit a9a01b0

Please sign in to comment.