@rjsf/validator-cfworker APIs
@rjsf/validator-cfworker is an additional, eval-free validator option for CSP-constrained applications. It is backed by @cfworker/json-schema, interprets schemas without eval or new Function, and defaults to JSON Schema draft 2020-12.
It is not an AJV replacement. See the package README for the unsupported-keyword matrix and current upstream-state notes.
Differences from @rjsf/validator-ajv8
- AJV-only options and extensions such as
AjvClass,$data,discriminator, andajv-errors'serrorMessageare not supported. $dynamicRefand$dynamicAnchorare not currently supported by the underlying engine.- Error messages differ from AJV, and there is no
ajv-i18nlocalizer equivalent. UsetransformErrorsto customize messages. - Precompiled-validator mode is not supported.
- The wrapper normalizes
undefinedform-data values before validation and caches validators by schema id to make repeated RJSFisValid()calls safe.
Types
The package exports CustomValidatorOptionsType, CFWorkerValidationError, CFWorkerFormatChecker, and SuppressDuplicateFilteringType from src/types.ts.
APIs
customizeValidator<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType>()
Creates a CFWorkerValidator<S, F>, an implementation of ValidatorType<S, F> backed by @cfworker/json-schema.
The validator keeps what it compiled for the last two root schemas it validated against, and drops the older one's when a third arrives.
Every form that uses the package's default export shares one validator, and so its cache: on a page with three or more such forms, moving between them compiles again.
A form given its own validator from customizeValidator() keeps its own cache.
Parameters
options.additionalMetaSchemas: additional schemas registered for cross-schema$refresolutionoptions.customFormats: format checkers expressed as functions, regular expressions, or regular-expression source stringsoptions.draft:'4','7','2019-09', or'2020-12'; defaults to'2020-12'options.extenderFn: hook called with every newly constructed engine validatoroptions.shortCircuit: stop at the first error; defaults tofalseoptions.suppressDuplicateFiltering:'none','anyOf','oneOf', or'all'
Returns
CFWorkerValidator<S, F>: the customized validator implementation
import { customizeValidator } from '@rjsf/validator-cfworker';
const validator = customizeValidator({
draft: '2020-12',
customFormats: { slug: /^[a-z0-9-]+$/ },
});