Skip to content

Commit c1d7e9c

Browse files
committed
Address validator-cfworker review feedback
1 parent 217ed9b commit c1d7e9c

11 files changed

Lines changed: 148 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ should change the heading of the (upcoming) version to include a major version b
1616
1717
-->
1818

19+
# 6.8.0
20+
21+
## @rjsf/validator-cfworker
22+
23+
- Added an eval-free, draft-2020-12 validator package backed by `@cfworker/json-schema`, including undefined form-data normalization and per-schema-id caching, fixing [#3269](https://github.com/rjsf-team/react-jsonschema-form/issues/3269) and contributing to [#4923](https://github.com/rjsf-team/react-jsonschema-form/issues/4923)
24+
25+
## Dev / docs / playground
26+
27+
- Added `@rjsf/validator-cfworker` to the playground validator selector and API documentation
28+
1929
# 6.7.1
2030

2131
## @rjsf/core
@@ -134,18 +144,13 @@ should change the heading of the (upcoming) version to include a major version b
134144
- Updated `transformRJSFValidationErrors()` to detect field name for `allOf` and `if-then-else` by falling back to the root schema to find the title, fixing [#5134](https://github.com/rjsf-team/react-jsonschema-form/issues/5134)
135145
- Updated the `ata-validator` dependency to `^1.0.0`, its first stable release; no API changes for `@rjsf/validator-ata` consumers ([#5157](https://github.com/rjsf-team/react-jsonschema-form/pull/5157))
136146

137-
## @rjsf/validator-cfworker
138-
139-
- Added an eval-free, draft-2020-12 validator package backed by `@cfworker/json-schema`, including undefined form-data normalization and per-schema-id caching, fixing [#3269](https://github.com/rjsf-team/react-jsonschema-form/issues/3269) and contributing to [#4923](https://github.com/rjsf-team/react-jsonschema-form/issues/4923)
140-
141147
## Dev / docs / playground
142148

143149
- Updated `@rjsf/snapshots` to add a test case to `formTests` that verifies the new Cycle detection UI
144150
- Updated the `custom-templates.md` and `custom-widgets-fields.md` for the new feature
145151
- Updated `utility-functions.md` to document `logUnsupportedDefaultForEnum()`
146152
- Updated the playground to add a selector for the `nestedDefaultsPrecedence` option
147153
- Replaced npm with pnpm v10 for development
148-
- Added `@rjsf/validator-cfworker` to the playground validator selector and API documentation
149154

150155
# 6.6.2
151156

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [@rjsf/utils](https://github.com/rjsf-team/react-jsonschema-form/tree/main/packages/utils)
4444
- [@rjsf/validator-ajv8](https://github.com/rjsf-team/react-jsonschema-form/tree/main/packages/validator-ajv8)
4545
- [@rjsf/validator-ata](https://github.com/rjsf-team/react-jsonschema-form/tree/main/packages/validator-ata)
46+
- [@rjsf/validator-cfworker](https://github.com/rjsf-team/react-jsonschema-form/tree/main/packages/validator-cfworker)
4647

4748
## Documentation
4849

packages/playground/src/app.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Theme as SuiTheme } from '@rjsf/semantic-ui';
1010
import { Theme as shadcnTheme } from '@rjsf/shadcn';
1111
import v8Validator, { customizeValidator } from '@rjsf/validator-ajv8';
1212
import ataValidator, { customizeValidator as customizeAtaValidator } from '@rjsf/validator-ata';
13-
import cfworkerValidator from '@rjsf/validator-cfworker';
13+
import cfworkerValidator, { customizeValidator as customizeCfworkerValidator } from '@rjsf/validator-cfworker';
1414
import localize_es from 'ajv-i18n/localize/es';
1515
import Ajv2019 from 'ajv/dist/2019.js';
1616
import Ajv2020 from 'ajv/dist/2020.js';
@@ -25,6 +25,8 @@ const AJV8_2020 = customizeValidator({ AjvClass: Ajv2020 });
2525
const AJV8_DISC = customizeValidator({ ajvOptionsOverrides: { discriminator: true } });
2626
const AJV8_DATA_REF = customizeValidator({ ajvOptionsOverrides: { $data: true } });
2727
const ataCoerce = customizeAtaValidator({ ataOptionsOverrides: { coerceTypes: true } });
28+
const cfworkerDraft7 = customizeCfworkerValidator({ draft: '7' });
29+
const cfworkerDraft2019 = customizeCfworkerValidator({ draft: '2019-09' });
2830

2931
const validators: PlaygroundProps['validators'] = {
3032
AJV8: v8Validator,
@@ -35,6 +37,8 @@ const validators: PlaygroundProps['validators'] = {
3537
'AJV8 (2020)': AJV8_2020,
3638
ATA: ataValidator,
3739
'ATA (coerceTypes)': ataCoerce,
40+
'CFWorker (Draft 7)': cfworkerDraft7,
41+
'CFWorker (2019-09)': cfworkerDraft2019,
3842
'CFWorker (2020-12)': cfworkerValidator,
3943
};
4044

packages/playground/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
},
6868
{
6969
"path": "../validator-ata"
70+
},
71+
{
72+
"path": "../validator-cfworker"
7073
}
7174
]
7275
}

packages/playground/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default defineConfig({
2929
'@rjsf/utils': path.resolve(__dirname, '../utils/src'),
3030
'@rjsf/validator-ajv8': path.resolve(__dirname, '../validator-ajv8/src'),
3131
'@rjsf/validator-ata': path.resolve(__dirname, '../validator-ata/src'),
32+
'@rjsf/validator-cfworker': path.resolve(__dirname, '../validator-cfworker/src'),
3233
'@rjsf/daisyui': path.resolve(__dirname, '../daisyui/src'),
3334
},
3435
},

packages/validator-cfworker/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# @rjsf/validator-cfworker
22

3-
`@rjsf/validator-cfworker` is an additional validator option for CSP-constrained RJSF users. It uses [`@cfworker/json-schema`](https://github.com/cfworker/cfworker/tree/main/packages/json-schema) to interpret schemas without `eval` or `new Function`, and defaults to JSON Schema draft 2020-12.
3+
`@rjsf/validator-cfworker` is an additional validator option for CSP-constrained RJSF users.
4+
It uses [`@cfworker/json-schema`](https://github.com/cfworker/cfworker/tree/main/packages/json-schema) to interpret schemas without `eval` or `new Function`, and defaults to JSON Schema draft 2020-12.
45

56
It is not an AJV replacement. Choose it when an eval-free runtime is more important than AJV-specific keywords, extensions, localization, or code generation.
67

packages/validator-cfworker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rjsf/validator-cfworker",
3-
"version": "6.6.2",
3+
"version": "6.7.1",
44
"main": "dist/index.js",
55
"module": "lib/index.js",
66
"typings": "lib/index.d.ts",

packages/validator-cfworker/src/createCfworkerInstance.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import cloneDeep from 'lodash/cloneDeep';
55

66
import type { CFWorkerFormatChecker, CustomValidatorOptionsType } from './types';
77

8+
/** Regular expression used to validate RJSF's `color` format. */
89
export const COLOR_FORMAT_REGEX =
910
/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/;
1011

12+
/** Regular expression used to validate RJSF's `data-url` format. */
1113
export const DATA_URL_FORMAT_REGEX = /^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;
1214

1315
interface FormatRegistryModule {
1416
format?: Record<string, CFWorkerFormatChecker>;
1517
}
1618

19+
/** Converts a supported custom-format declaration into a format checker.
20+
*
21+
* @param spec - The custom-format function, regular expression, or regular-expression source
22+
* @returns - A function that checks whether a string satisfies the format
23+
*/
1724
function asFormatChecker(spec: string | RegExp | CFWorkerFormatChecker): CFWorkerFormatChecker {
1825
if (typeof spec === 'function') {
1926
return spec;
@@ -22,6 +29,11 @@ function asFormatChecker(spec: string | RegExp | CFWorkerFormatChecker): CFWorke
2229
return (value: string) => regex.test(value);
2330
}
2431

32+
/** Installs the built-in RJSF formats and any user-provided formats into the engine registry.
33+
*
34+
* @param customFormats - The custom formats to add after the built-in `color` and `data-url` formats
35+
* @param [targetRegistry] - The format registry to update; defaults to the `@cfworker/json-schema` module registry
36+
*/
2537
export function installFormats(
2638
customFormats: CustomValidatorOptionsType['customFormats'],
2739
targetRegistry = (CFWorkerJsonSchema as unknown as FormatRegistryModule).format,
@@ -41,7 +53,13 @@ export function installFormats(
4153
}
4254
}
4355

44-
/** Builds a schema-bound `@cfworker/json-schema` validator. */
56+
/** Builds a schema-bound `@cfworker/json-schema` validator.
57+
*
58+
* @param schema - The schema the validator will use
59+
* @param [options={}] - The options used to configure formats, schemas, draft behavior, and engine extension
60+
* @param [rootSchema] - The root schema to register for cross-schema `$ref` resolution
61+
* @returns - The configured schema-bound validator
62+
*/
4563
export default function createCfworkerInstance(
4664
schema: Schema | boolean,
4765
options: CustomValidatorOptionsType = {},

packages/validator-cfworker/src/processRawValidationErrors.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,36 @@ export interface RawValidationErrorsType<Result = any> {
2828
validationError?: Error;
2929
}
3030

31+
/** Converts a cfworker instance location into RJSF's dot-separated property path.
32+
*
33+
* @param instanceLocation - The instance location emitted by `@cfworker/json-schema`
34+
* @returns - The corresponding RJSF property path
35+
*/
3136
function instanceLocationToProperty(instanceLocation: string): string {
3237
if (!instanceLocation || instanceLocation === '#') {
3338
return '';
3439
}
3540
return instanceLocation.replace(/^#?/, '').replace(/\//g, '.');
3641
}
3742

43+
/** Extracts the missing property name from a cfworker `required` error.
44+
*
45+
* @param error - The cfworker validation error to inspect
46+
* @returns - The missing property name when the error describes one
47+
*/
3848
function extractMissingProperty(error: CFWorkerValidationError): string | undefined {
3949
if (error.keyword !== 'required') {
4050
return undefined;
4151
}
4252
return error.error.match(/required property ["']([^"']+)["']/)?.[1];
4353
}
4454

55+
/** Filters duplicate errors from `anyOf`/`oneOf` schema paths according to the `suppressDuplicateFiltering` flag.
56+
*
57+
* @param errorList - The list of RJSF validation errors to filter
58+
* @param [suppressDuplicateFiltering='none'] - Controls which duplicate filtering is suppressed
59+
* @returns - The filtered list of validation errors
60+
*/
4561
export function filterDuplicateErrors(
4662
errorList: RJSFValidationError[],
4763
suppressDuplicateFiltering: SuppressDuplicateFilteringType = 'none',
@@ -69,7 +85,14 @@ export function filterDuplicateErrors(
6985
}, []);
7086
}
7187

72-
/** Converts `@cfworker/json-schema` output units into RJSF validation errors. */
88+
/** Converts `@cfworker/json-schema` output units into RJSF validation errors.
89+
*
90+
* @param errors - The cfworker errors to convert
91+
* @param [uiSchema] - The uiSchema used to resolve field titles
92+
* @param [suppressDuplicateFiltering] - Controls which duplicate filtering is suppressed
93+
* @param [schema] - The schema used to resolve field titles
94+
* @returns - The converted RJSF validation errors
95+
*/
7396
export function transformRJSFValidationErrors<
7497
T = any,
7598
S extends StrictRJSFSchema = RJSFSchema,
@@ -149,6 +172,18 @@ export function transformRJSFValidationErrors<
149172
return filterDuplicateErrors(errorList, suppressDuplicateFiltering);
150173
}
151174

175+
/** Processes raw validation errors and applies optional error transforms and custom validation.
176+
*
177+
* @param validator - The validator used to derive default form state for custom validation
178+
* @param rawErrors - The raw cfworker errors and any engine exception
179+
* @param formData - The form data being validated
180+
* @param schema - The schema against which the form data is validated
181+
* @param [customValidate] - A function that adds application-specific validation errors
182+
* @param [transformErrors] - A function that transforms errors before custom validation
183+
* @param [uiSchema] - The uiSchema passed to error transformation and custom validation
184+
* @param [suppressDuplicateFiltering] - Controls which duplicate filtering is suppressed
185+
* @returns - The processed validation errors and error schema
186+
*/
152187
export default function processRawValidationErrors<
153188
T = any,
154189
S extends StrictRJSFSchema = RJSFSchema,

packages/validator-cfworker/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { OutputUnit, Schema, SchemaDraft, Validator } from '@cfworker/json-schema';
22

3+
/** The type describing a function that checks whether a string satisfies a custom format */
34
export type CFWorkerFormatChecker = (value: string) => boolean;
45

6+
/** The type describing the value for the `suppressDuplicateFiltering` option */
57
export type SuppressDuplicateFilteringType = 'anyOf' | 'oneOf' | 'all' | 'none';
68

79
/** Options for the `@cfworker/json-schema` backed validator. */

0 commit comments

Comments
 (0)