Repository navigation
feat(core): add a pluggable Form Validator next to Ajv - #2644
Open
gigibiffi84 wants to merge 1 commit into
Open
gigibiffi84 wants to merge 1 commit into
gigibiffi84 wants to merge 1 commit into
Conversation
Adds the validation seam from issue eclipsesource#1498 as a non-breaking addition: the `validator` init/updateCore option takes a FormValidatorFactory `(schema) => FormValidator` or a FormValidator already bound to the schema. The types mirror the FormValidator / ValidationIssue of the 4.x presentation-model branch, restricted to synchronous results. - The core reducer creates the Form Validator through the factory whenever the schema changes and caches it in `state.formValidator`; `state.validator` keeps holding the compiled Ajv function when Ajv validates, so existing consumers are unaffected. - Issues from custom validators are converted to the Ajv error shape the rest of core and the renderers read (`issuesToErrors`): missing `key` becomes `custom`, `required` issues addressed to the missing property are split into parent path plus `params.missingProperty`, non-error severities are dropped and a missing `parentSchema` is resolved from the form schema. - `createAjvValidator(ajv?)` is the built-in adapter; `compiledAjvValidator(validateFn)` wraps Ajv standalone code for CSP setups without unsafe-eval. - `getValidator(state)` exposes the bound Form Validator. - Switching to NoValidation via setValidationMode now also drops the cached validator, as init and updateCore already did. Precedence: `validator` wins, otherwise `ajv`, otherwise the default Ajv instance. Existing users notice nothing. Part of eclipsesource#1498 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
✅ Deploy Preview for jsonforms-examples ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #1498. Follows the agreement to land validator decoupling incrementally and non-breaking in 3.x, shaped after the
FormValidator/ValidationIssuetypes onfeat/presentation-model(#2571).What changed
@jsonforms/coregains a pluggable validation seam next to Ajv:InitActionOptions.validator: aFormValidatorFactory(schema) => FormValidator, or aFormValidatoralready bound to the schema. Precedence:validator, thenajv, then the default Ajv instance.FormValidatorhasvalidate(data): ValidationIssue[](synchronous in 3.x) and an optionalmatches(schema, data)for rule conditions (wired up in a follow-up PR).ValidationIssuemirrors 4.x (path,key,message,severity) plus optionalparamsandparentSchema.state.formValidator.state.validatorkeeps holding the compiled Ajv function whenever Ajv validates.issuesToErrorsconverts issues from custom validators to the Ajv error shape the rest of core and the renderers read: missingkeybecomescustom, arequiredissue addressed to the missing property is split into parent path plusparams.missingProperty, non-error severities are dropped, and a missingparentSchemais resolved from the form schema by path. Ajv-backed validators bypass the conversion, so Ajv users get identical errors.createAjvValidator(ajv?);compiledAjvValidator(validateFn)wraps Ajv standalone code for CSP setups that keep Ajv but cannot allowunsafe-eval.getValidator(state).For adopters
Nothing changes without the new option: all existing core tests pass unchanged. One small fix:
setValidationMode('NoValidation')now also drops the cached validator, asinitandupdateCorealready did; previously a later data update could reintroduce errors in that mode.Tests
test/util/formValidator.test.ts: lossless Ajv round trip, every normalization rule,resolveParentSchemaover properties, items, additionalProperties and$ref, both adapters, guards.test/reducers/core.test.ts: a fake validator receives every compile and validation, Ajv is never asked to compile when a validator is given, bound validators,updateCorerecompiling only on schema change, options without avalidatorkey keeping the current validator, explicitundefinedswitching back to Ajv,update,setSchema,NoValidation,setAjvprecedence, issue normalization,getValidator.@jsonforms/react,@jsonforms/vueand@jsonforms/angularbuild against the new types; typedoc succeeds.Follow-ups (separate PRs)
Rule conditions through
matches/ the factory; thevalidatorprop in the React, Angular and Vue bindings with an example; a Structural Matcher fallback for rules.🤖 Generated with Claude Code