Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/parser/jsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,11 @@ function validate(n: ASTNode | undefined, schema: JSONSchema, validationResult:
const testAlternatives = (alternatives: JSONSchemaRef[], maxOneMatch: boolean) => {
const matches = [];

const alternativesToTest = _tryDiscriminatorOptimization(alternatives) ?? alternatives;

// remember the best match that is used for error messages
let bestMatch: { schema: JSONSchema; validationResult: ValidationResult; matchingSchemas: ISchemaCollector; } | undefined = undefined;
for (const subSchemaRef of alternatives) {
for (const subSchemaRef of alternativesToTest) {
const subSchema = asSchema(subSchemaRef);
const subValidationResult = new ValidationResult();
const subMatchingSchemas = matchingSchemas.newSub();
Expand Down Expand Up @@ -620,7 +622,77 @@ function validate(n: ASTNode | undefined, schema: JSONSchema, validationResult:
}
}

function _tryDiscriminatorOptimization(alternatives: JSONSchemaRef[]): JSONSchemaRef[] | undefined {
if (alternatives.length < 2) {
return undefined;
}

const buildConstMap = (getSchemas: (alt: JSONSchema, idx: number) => [string | number, JSONSchema][] | undefined) => {
const constMap = new Map<string | number, Map<any, number[]>>();

for (let i = 0; i < alternatives.length; i++) {
const schemas = getSchemas(asSchema(alternatives[i]), i);
if (!schemas) {
return undefined; // Early exit if any alternative can't be processed
}

schemas.forEach(([key, schema]) => {
if (schema.const !== undefined) {
if (!constMap.has(key)) {
constMap.set(key, new Map());
}
const valueMap = constMap.get(key)!;
if (!valueMap.has(schema.const)) {
valueMap.set(schema.const, []);
}
valueMap.get(schema.const)!.push(i);
}
});
}
return constMap;
};

const findDiscriminator = (constMap: Map<string | number, Map<any, number[]>>, getValue: (key: string | number) => any) => {
for (const [key, valueMap] of constMap) {
const coveredAlts = new Set<number>();
valueMap.forEach(indices => indices.forEach(idx => coveredAlts.add(idx)));

if (coveredAlts.size === alternatives.length) {
const discriminatorValue = getValue(key);
const matchingIndices = valueMap.get(discriminatorValue);
if (matchingIndices?.length) {
return matchingIndices.map(idx => alternatives[idx]);
}
break; // Found valid discriminator but no match
}
}
return undefined;
};

if (node.type === 'object' && node.properties?.length) {
const constMap = buildConstMap((schema) =>
schema.properties ? Object.entries(schema.properties).map(([k, v]) => [k, asSchema(v)]) : undefined
);
if (constMap) {
return findDiscriminator(constMap, (propName) => {
const prop = node.properties.find(p => p.keyNode.value === propName);
return prop?.valueNode?.type === 'string' ? prop.valueNode.value : undefined;
});
}
} else if (node.type === 'array' && node.items?.length) {
const constMap = buildConstMap((schema) => {
const itemSchemas = schema.prefixItems || (Array.isArray(schema.items) ? schema.items : undefined);
return itemSchemas ? itemSchemas.map((item, idx) => [idx, asSchema(item)]) : undefined;
});
if (constMap) {
return findDiscriminator(constMap, (itemIndex) => {
const item = node.items[itemIndex as number];
return item?.type === 'string' ? item.value : undefined;
});
}
}
return undefined;
}

function _validateNumberNode(node: NumberASTNode): void {
const val = node.value;
Expand Down
Loading