@rjsf/validator-ajv8
Advanced tools
Comparing version 5.18.4 to 5.18.5
@@ -218,2 +218,15 @@ "use strict"; | ||
} | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema) { | ||
const rootSchemaId = rootSchema[import_utils3.ID_KEY] ?? import_utils3.ROOT_SCHEMA_PREFIX; | ||
if (this.ajv.getSchema(rootSchemaId) === void 0) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} else if (!(0, import_utils3.deepEquals)(rootSchema, this.ajv.getSchema(rootSchemaId)?.schema)) { | ||
this.ajv.removeSchema(rootSchemaId); | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -228,5 +241,4 @@ * false otherwise. If the schema is invalid, then this function will return | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema[import_utils3.ID_KEY] ?? import_utils3.ROOT_SCHEMA_PREFIX; | ||
try { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
this.handleSchemaUpdate(rootSchema); | ||
const schemaWithIdRefPrefix = (0, import_utils3.withIdRefPrefix)(schema); | ||
@@ -244,4 +256,2 @@ const schemaId = schemaWithIdRefPrefix[import_utils3.ID_KEY] ?? (0, import_utils3.hashForSchema)(schemaWithIdRefPrefix); | ||
return false; | ||
} finally { | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -248,0 +258,0 @@ } |
// src/validator.ts | ||
import { | ||
deepEquals, | ||
ID_KEY, | ||
@@ -194,2 +195,15 @@ ROOT_SCHEMA_PREFIX, | ||
} | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema) { | ||
const rootSchemaId = rootSchema[ID_KEY] ?? ROOT_SCHEMA_PREFIX; | ||
if (this.ajv.getSchema(rootSchemaId) === void 0) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} else if (!deepEquals(rootSchema, this.ajv.getSchema(rootSchemaId)?.schema)) { | ||
this.ajv.removeSchema(rootSchemaId); | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -204,5 +218,4 @@ * false otherwise. If the schema is invalid, then this function will return | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema[ID_KEY] ?? ROOT_SCHEMA_PREFIX; | ||
try { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
this.handleSchemaUpdate(rootSchema); | ||
const schemaWithIdRefPrefix = withIdRefPrefix(schema); | ||
@@ -220,4 +233,2 @@ const schemaId = schemaWithIdRefPrefix[ID_KEY] ?? hashForSchema(schemaWithIdRefPrefix); | ||
return false; | ||
} finally { | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -224,0 +235,0 @@ } |
@@ -175,2 +175,15 @@ (function (global, factory) { | ||
} | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema) { | ||
const rootSchemaId = rootSchema[utils.ID_KEY] ?? utils.ROOT_SCHEMA_PREFIX; | ||
if (this.ajv.getSchema(rootSchemaId) === void 0) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} else if (!utils.deepEquals(rootSchema, this.ajv.getSchema(rootSchemaId)?.schema)) { | ||
this.ajv.removeSchema(rootSchemaId); | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -185,5 +198,4 @@ * false otherwise. If the schema is invalid, then this function will return | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema[utils.ID_KEY] ?? utils.ROOT_SCHEMA_PREFIX; | ||
try { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
this.handleSchemaUpdate(rootSchema); | ||
const schemaWithIdRefPrefix = utils.withIdRefPrefix(schema); | ||
@@ -201,4 +213,2 @@ const schemaId = schemaWithIdRefPrefix[utils.ID_KEY] ?? utils.hashForSchema(schemaWithIdRefPrefix); | ||
return false; | ||
} finally { | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -205,0 +215,0 @@ } |
@@ -51,2 +51,7 @@ import Ajv from 'ajv'; | ||
validateFormData(formData: T | undefined, schema: S, customValidate?: CustomValidator<T, S, F>, transformErrors?: ErrorTransformer<T, S, F>, uiSchema?: UiSchema<T, S, F>): ValidationData<T>; | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema: S): void; | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -53,0 +58,0 @@ * false otherwise. If the schema is invalid, then this function will return |
@@ -1,2 +0,2 @@ | ||
import { ID_KEY, ROOT_SCHEMA_PREFIX, toErrorList, withIdRefPrefix, hashForSchema, } from '@rjsf/utils'; | ||
import { deepEquals, ID_KEY, ROOT_SCHEMA_PREFIX, toErrorList, withIdRefPrefix, hashForSchema, } from '@rjsf/utils'; | ||
import createAjvInstance from './createAjvInstance'; | ||
@@ -77,2 +77,20 @@ import processRawValidationErrors from './processRawValidationErrors'; | ||
} | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema) { | ||
var _a, _b; | ||
const rootSchemaId = (_a = rootSchema[ID_KEY]) !== null && _a !== void 0 ? _a : ROOT_SCHEMA_PREFIX; | ||
// add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// if schema validator instance doesn't exist, add it. | ||
// else if the root schemas don't match, we should remove and add the root schema so we don't have to remove and recompile the schema every run. | ||
if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
else if (!deepEquals(rootSchema, (_b = this.ajv.getSchema(rootSchemaId)) === null || _b === void 0 ? void 0 : _b.schema)) { | ||
this.ajv.removeSchema(rootSchemaId); | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -87,15 +105,10 @@ * false otherwise. If the schema is invalid, then this function will return | ||
isValid(schema, formData, rootSchema) { | ||
var _a, _b; | ||
const rootSchemaId = (_a = rootSchema[ID_KEY]) !== null && _a !== void 0 ? _a : ROOT_SCHEMA_PREFIX; | ||
var _a; | ||
try { | ||
// add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
this.handleSchemaUpdate(rootSchema); | ||
// then rewrite the schema ref's to point to the rootSchema | ||
// this accounts for the case where schema have references to models | ||
// that lives in the rootSchema but not in the schema in question. | ||
// if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
// TODO restore the commented out `if` above when the TODO in the `finally` is completed | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
// } | ||
const schemaWithIdRefPrefix = withIdRefPrefix(schema); | ||
const schemaId = (_b = schemaWithIdRefPrefix[ID_KEY]) !== null && _b !== void 0 ? _b : hashForSchema(schemaWithIdRefPrefix); | ||
const schemaId = (_a = schemaWithIdRefPrefix[ID_KEY]) !== null && _a !== void 0 ? _a : hashForSchema(schemaWithIdRefPrefix); | ||
let compiledValidator; | ||
@@ -118,9 +131,4 @@ compiledValidator = this.ajv.getSchema(schemaId); | ||
} | ||
finally { | ||
// TODO: A function should be called if the root schema changes so we don't have to remove and recompile the schema every run. | ||
// make sure we remove the rootSchema from the global ajv instance | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=validator.js.map |
{ | ||
"name": "@rjsf/validator-ajv8", | ||
"version": "5.18.4", | ||
"version": "5.18.5", | ||
"main": "dist/index.js", | ||
@@ -51,3 +51,3 @@ "module": "lib/index.js", | ||
"@babel/preset-typescript": "^7.23.3", | ||
"@rjsf/utils": "^5.18.4", | ||
"@rjsf/utils": "^5.18.5", | ||
"@types/jest": "^29.5.12", | ||
@@ -86,3 +86,3 @@ "@types/json-schema": "^7.0.15", | ||
"license": "Apache-2.0", | ||
"gitHead": "63dbb6b8c98faa1c776cc6a66d47732a2be49cdf" | ||
"gitHead": "d5ef055f1b40c8837c13126777478bfc30dc9ade" | ||
} |
import Ajv, { ErrorObject, ValidateFunction } from 'ajv'; | ||
import { | ||
CustomValidator, | ||
deepEquals, | ||
ErrorSchema, | ||
@@ -122,2 +123,19 @@ ErrorTransformer, | ||
/** | ||
* This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
handleSchemaUpdate(rootSchema: S): void { | ||
const rootSchemaId = rootSchema[ID_KEY] ?? ROOT_SCHEMA_PREFIX; | ||
// add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// if schema validator instance doesn't exist, add it. | ||
// else if the root schemas don't match, we should remove and add the root schema so we don't have to remove and recompile the schema every run. | ||
if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} else if (!deepEquals(rootSchema, this.ajv.getSchema(rootSchemaId)?.schema)) { | ||
this.ajv.removeSchema(rootSchemaId); | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
} | ||
/** Validates data against a schema, returning true if the data is valid, or | ||
@@ -132,12 +150,7 @@ * false otherwise. If the schema is invalid, then this function will return | ||
isValid(schema: S, formData: T | undefined, rootSchema: S) { | ||
const rootSchemaId = rootSchema[ID_KEY] ?? ROOT_SCHEMA_PREFIX; | ||
try { | ||
// add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
this.handleSchemaUpdate(rootSchema); | ||
// then rewrite the schema ref's to point to the rootSchema | ||
// this accounts for the case where schema have references to models | ||
// that lives in the rootSchema but not in the schema in question. | ||
// if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
// TODO restore the commented out `if` above when the TODO in the `finally` is completed | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
// } | ||
const schemaWithIdRefPrefix = withIdRefPrefix<S>(schema) as S; | ||
@@ -160,8 +173,4 @@ const schemaId = schemaWithIdRefPrefix[ID_KEY] ?? hashForSchema(schemaWithIdRefPrefix); | ||
return false; | ||
} finally { | ||
// TODO: A function should be called if the root schema changes so we don't have to remove and recompile the schema every run. | ||
// make sure we remove the rootSchema from the global ajv instance | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
320998
2675