Skip to content

Commit

Permalink
refactor: consistently use validate-node api
Browse files Browse the repository at this point in the history
  • Loading branch information
sagold committed Apr 14, 2024
1 parent a41e7e2 commit 0aad686
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions lib/draft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,13 @@ export class Draft {
* Returns the json-schema of the given object property or array item.
* e.g. it steps by one key into the data
*
* This helper determines the location of the property within the schema (additional properties, oneOf, ...) and
* returns the correct schema.
* This helper determines the location of the property within the schema (additional properties, oneOf, ...) and
* returns the correct schema.
*
* @param node
* @param key - property-name or array-index
* @param schema - json schema of current data
* @param data - parent of key
* @param [pointer] - pointer to schema and data (parent of key)
* @return Schema or Error if failed resolving key
* @return schema-node containing child schema or error if failed resolving key
*/
step(node: SchemaNode, key: string | number, data: any): SchemaNode | JsonError {
return this.config.step(node, key, data);
Expand Down
4 changes: 1 addition & 3 deletions lib/draft2019/validation/keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function isPropertyEvaluated(schemaNode: SchemaNode, propertyName: string, value
// ADDITIONAL-PROPERTIES
if (isObject(schema.additionalProperties)) {
const nextSchema = schema.additionalProperties;
return node.draft.validate(value, nextSchema);
return node.draft.validate(node.next(nextSchema), value);
}
return false;
}
Expand Down Expand Up @@ -186,8 +186,6 @@ const KeywordValidation: Record<string, JsonValidator> = {
});
}
});
// const errors = draft.validate(value, { ...schema, unevaluatedItems: undefined }, pointer);
// return errors.map(e => draft.errors.unevaluatedItemsError({ ...e.data }));
}

const errors: JsonError[] = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/features/oneOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function resolveOneOf(node: SchemaNode, data: any): SchemaNode | JsonErro
return resultNode;
}

let result = flattenArray(draft.validate(oneOfValue, resultNode.schema, pointer));
let result = flattenArray(draft.validate(resultNode, oneOfValue));
result = result.filter(errorOrPromise);

if (result.length > 0) {
Expand Down
3 changes: 2 additions & 1 deletion lib/isValid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export default function isValid(
schema: JsonSchema = draft.rootSchema,
pointer: JsonPointer = "#"
): boolean {
return draft.validate(value, schema, pointer).length === 0;
const node = draft.createNode(schema, pointer);
return draft.validate(node, value).length === 0;
}
2 changes: 1 addition & 1 deletion lib/validateAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function validateAsync(
): Promise<Array<JsonError>> {
const { schema, pointer, onError } = { schema: draft.rootSchema, pointer: "#", ...options };

let errors: Array<JsonError> = draft.validate(value, schema, pointer);
let errors: Array<JsonError> = draft.validate(draft.createNode(schema, pointer), value);
if (onError) {
errors = flattenArray(errors);
const notifyError = createErrorNotification(onError);
Expand Down

0 comments on commit 0aad686

Please sign in to comment.