Precompiled validators miss sub-schemas merged with a customMergeAllOf
Prerequisites
- I have searched the existing issues
- I understand that providing a SSCCE example is tremendously useful to the maintainers.
- I have read the documentation
- Ideally, I'm providing a sample JSFiddle, Codesandbox.io or preferably a shared playground link demonstrating the issue.
What theme are you using?
validator-ajv8
Version
v7 branch (also affects @rjsf/validator-ata)
Current Behavior
A precompiled validator can throw No precompiled validator function was found for the given schema when the form passes a customMergeAllOf.
schemaParser() collects the sub-schemas that isValid() will be called with, and compileSchemaValidators()/compileSchemaValidatorsCode() precompile a validator for each of them by hash. The parser walks the schema with expandAllBranches, which leaves allOfs unmerged, with one exception: a named property that also matches a patternProperties entry is merged with its matching patterns (retrieveSchemaInternal() merges { allOf: [property, ...patterns] }). The parser has no way to receive a customMergeAllOf, so it merges with the default merge. At runtime the form merges with its customMergeAllOf, and when that produces different sub-schemas (e.g. the options of a oneOf inside the merged property), their hashes are missing from the precompiled map.
Found while working on #4385, which threads customMergeAllOf through everything else.
Expected Behavior
Precompiled validators can be generated for the customMergeAllOf the form will use, so every sub-schema the form validates against has a precompiled function.
Steps To Reproduce
On the v7 branch (SchemaContext API from #4385):
import type { RJSFSchema } from '@rjsf/utils';
import { getDefaultFormState, mergeSchemas } from '@rjsf/utils';
import { compileSchemaValidatorsCode } from '@rjsf/validator-ajv8/compileSchemaValidators';
import { createPrecompiledValidator } from '@rjsf/validator-ajv8';
// `p` is both a named property and a patternProperties match, so `{ allOf: [p, pattern] }` gets merged
const rootSchema: RJSFSchema = {
type: 'object',
properties: { p: { type: 'object', properties: { x: { type: 'string' } } } },
patternProperties: {
'^p$': { properties: { choice: { oneOf: [{ const: 'a' }, { const: 'b' }] } } },
},
};
// A custom merge whose result differs from the default one inside the merged subschema
const customMergeAllOf = (schema: RJSFSchema): RJSFSchema => {
const { allOf, ...rest } = schema;
const merged = (allOf as RJSFSchema[]).reduce((acc, s) => mergeSchemas(acc, s) as RJSFSchema, rest);
const choice = (merged.properties as any)?.choice;
if (choice?.oneOf) {
choice.oneOf = choice.oneOf.map((o: RJSFSchema) => ({ ...o, title: `Option ${o.const}` }));
}
return merged;
};
const validator = createPrecompiledValidator(loadModule(compileSchemaValidatorsCode(rootSchema)), rootSchema);
const formData = { p: { choice: 'b' } };
getDefaultFormState({ validator }, { schema: rootSchema, rootSchema, formData }); // fine
// Error: No precompiled validator function was found for the given schema for "1691cd08"
getDefaultFormState({ validator, customMergeAllOf }, { schema: rootSchema, rootSchema, formData });(loadModule evaluates the generated module code, as test/harness/compileSuperSchema.ts#evalValidatorCode does.)
Proposed fix
An optional, non-breaking customMergeAllOf parameter, prototyped and verified against the repro above:
// @rjsf/utils: parseSchema builds one { validator, customMergeAllOf } SchemaContext and passes it down
schemaParser(rootSchema, customMergeAllOf?)
// @rjsf/validator-ajv8 and @rjsf/validator-ata
compileSchemaValidatorsCode(schema, options?, customMergeAllOf?)
compileSchemaValidators(schema, output, options?, customMergeAllOf?)Compiling with the same customMergeAllOf the form uses makes the repro pass; compiling without it behaves as today.
Open questions:
- A trailing parameter keeps
customMergeAllOfout ofCustomValidatorOptionsType, whichcustomizeValidator()also takes and where it would mean nothing. The alternative is a field on that options object, at the cost of onecustomizeValidator()ignores. - The build script has to import the same merge function the form passes, or the miss comes back; the precompiled-validator docs in
validation.mdshould say so.
Remaining work: the repro as a test in ajv8 and ata, the validation.md note, and changelog entries for utils, ajv8 and ata.
Environment
- **OS**: macOS (Darwin 25.6.0)
- **Node**: 24.21.0Source: rjsf-team/react-jsonschema-form