@rjsf/validator-ata APIs
@rjsf/validator-ata is an alternative to @rjsf/validator-ajv8, backed by ata-validator instead of AJV.
The public surface mirrors the AJV package, so swapping the import is enough for the form to keep working: customizeValidator(), ValidatorType<S, F>, custom formats, transformErrors, customValidate, and suppressDuplicateFiltering all behave the same.
See the Validation documentation for examples of using these APIs.
Differences from @rjsf/validator-ajv8
- AJV-only options (
AjvClass,ajvFormatOptions,ajvOptionsOverrides) are not accepted. The closest equivalent isataOptionsOverrides, which is spread on top of the defaultata-validatoroptions. - The
ata-validatorformat set is always installed, so there is no opt-in flag for formats. - A
localizergets copies of the errors with the property names in theirmissingPropertyandpropertyparams quoted, such as'foo', so that the validator can replace them in its messages with the property'stitleorui:title. Only themessageit sets on each error is kept. - Precompiled validators (
compileSchemaValidators/createPrecompiledValidator) are supported.ata-validator'sbundleStandaloneoutput is adapted into the validator function map the precompiled consumer expects. Error output matches the non-precompiled validator, including per-field errors for schema-valuedadditionalProperties(withata-validator>= 0.17.4); an invalidanyOfreports a single error on the field, the same as the runtime path. One constraint carries over from the standalone (AOT) path: custom formats must be a RegExp or pre-anchored string pattern, since a function checker cannot be serialized into the bundle and is rejected at compile time.ata-validatorcompiles each schema as a document of its own and resolves a$refonly to an entry of thedefinitionsor$defsin it, so each schema is compiled as a document carrying every subschema of the root it reaches through a$ref, such as a definition, a property or the root itself, as a definition of its own in one container, the$refrenamed to it. A$refbeside other keywords is compiled as anallOfentry beside them. A$refthat names nothing in the root, or one into another document, leaves a schema with no compiled function, which throwswas not compiled to a standalone validatorwhen the form validates against it.
Types
There are a few TypeScript types that are exported by @rjsf/validator-ata in support of the APIs. These types can be found on GitHub here. The package also re-exports ata-validator's ValidationError, the type of the errors rawValidation() returns, so an app doesn't need ata-validator as its own dependency to name it.
APIs
customizeValidator<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType>()
Creates and returns an ATAValidator, an implementation of ValidatorType customized with the given options if provided.
If a localizer is provided, it is used to translate the messages generated by the underlying ata-validator validation.
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={}]: CustomValidatorOptionsType - The optional map of
CustomValidatorOptionsTypeoptions that are used to create theValidatorTypeinstance. Supported options:additionalMetaSchemas: Additional schemas registered for cross-schema$refresolutioncustomFormats: Custom format checkers; values can be functions, RegExps, or pre-anchored regex source stringsataOptionsOverrides: Overrides spread on top of the defaultata-validatoroptions (e.g.coerceTypes,removeAdditional,verbose,abortEarly)extenderFn: A function called against the constructedValidatorinstance for additional setup; returning a different instance is supportedsuppressDuplicateFiltering: Controls filtering of duplicateanyOf/oneOferrors using theSuppressDuplicateFilteringTypetype.'none'(default) filters duplicates for both;'anyOf'suppresses filtering foranyOf(oneOf duplicates still filtered);'oneOf'suppresses filtering foroneOf(anyOf duplicates still filtered);'all'disables all duplicate filtering.
- [localizer]: Localizer | undefined - If provided, is used to localize a list of
ata-validatorValidationErrors after running the form validation
Returns
- ATAValidator<S, F>: The custom validator implementation resulting from the set of parameters provided
compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>()
Compiles a schema into an output file that can be loaded later as a precompiled validator.
The main reasons for using a precompiled validator are reducing code size, improving startup time by skipping schema compilation, and avoiding dynamic code generation when a browser's Content Security Policy forbids it.
Under the hood this calls ata-validator's bundleStandalone and wraps the result in the map the precompiled consumer expects.
NOTE: Custom formats passed through
options.customFormatsmust be a RegExp or a pre-anchored string pattern. A function checker cannot be serialized into the standalone bundle and is rejected at compile time.
Parameters
- schema: S - The schema to be compiled into a set of precompiled validator functions
- output: string - The name of the file into which the precompiled validator functions are generated
- [options={}]: CustomValidatorOptionsType - The options accepted by
customizeValidator(). The schemas compiled are the onesgetValidationSchemas()lists. ThecustomizeValidator()options have limits:extenderFn, extra schemas without an$idand theataOptionsOverrideskeyscoerceTypes,removeAdditionalandkeywordscannot be carried into the generated file and are ignored with a warning, the generated file never fills indefaultvalues, andsuppressDuplicateFilteringbelongs tocreatePrecompiledValidator(). See the precompiled section ofusage/validation.md
createPrecompiledValidator<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType>()
Creates and returns an ATAPrecompiledValidator, an implementation of ValidatorType that uses the precompiled validator functions.
If a localizer is provided, it is used to translate the messages generated by the underlying ata-validator validation.
NOTE: The
validateFnsparameter is an object obtained by importing from a precompiled validation file created via thecompileSchemaValidators()function.
Parameters
- validateFns: ValidatorFunctions - The map of the validation functions that are created by the
compileSchemaValidators()function - rootSchema: S - The root schema that was used with the
compileSchemaValidators()function - [options]:
PrecompiledValidatorOptionsType<S> | undefined- The options the validator is constructed with: alocalizer, used to localize the engine's errors; asuppressDuplicateFiltering, controlling filtering of duplicateanyOf/oneOferrors (seecustomizeValidator()); and the form'scustomMergeAllOf, used when the validator resolves its own root schema to check the schema it is handed against it
Returns
- ATAPrecompiledValidator<S, F>: The precompiled validator implementation resulting from the set of parameters provided, a
ValidatorTypewhoserawValidation()reportsata-validator'sValidationErrors