@rjsf/validator-ajv8
Advanced tools
Comparing version 5.0.0-beta.12 to 5.0.0-beta.13
import * as _rjsf_utils from '@rjsf/utils'; | ||
import { StrictRJSFSchema, RJSFSchema, ValidatorType } from '@rjsf/utils'; | ||
import Ajv, { Options, ErrorObject } from 'ajv'; | ||
import Ajv, { Options, ErrorObject } from 'ajv8'; | ||
import { FormatsPluginOptions } from 'ajv-formats'; | ||
@@ -5,0 +5,0 @@ |
@@ -9,3 +9,3 @@ 'use strict'; | ||
var utils = require('@rjsf/utils'); | ||
var Ajv = require('ajv'); | ||
var Ajv = require('ajv8'); | ||
var addFormats = require('ajv-formats'); | ||
@@ -23,3 +23,4 @@ | ||
allErrors: true, | ||
multipleOfPrecision: 8 | ||
multipleOfPrecision: 8, | ||
strict: false | ||
}; | ||
@@ -41,2 +42,3 @@ const COLOR_FORMAT_REGEX = /^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/; | ||
* @param [ajvFormatOptions] - The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it | ||
* @param [AjvClass] - The `Ajv` class to use when creating the validator instance | ||
*/ | ||
@@ -54,4 +56,6 @@ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
if (ajvFormatOptions) { | ||
addFormats__default["default"](ajv, ajvFormatOptions); | ||
} else if (ajvFormatOptions !== false) { | ||
addFormats__default["default"](ajv); | ||
} | ||
@@ -61,2 +65,5 @@ // add custom formats | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// Add RJSF-specific additional properties keywords so Ajv doesn't report errors if strict is enabled. | ||
ajv.addKeyword(utils.ADDITIONAL_PROPERTY_FLAG); | ||
ajv.addKeyword(utils.RJSF_ADDITONAL_PROPERTIES_FLAG); | ||
// add more schemas to validate against | ||
@@ -283,17 +290,27 @@ if (Array.isArray(additionalMetaSchemas)) { | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
let compilationError = undefined; | ||
let compiledValidator; | ||
if (schema["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schema["$id"]); | ||
} | ||
try { | ||
this.ajv.validate(schema, formData); | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schema); | ||
} | ||
compiledValidator(formData); | ||
} catch (err) { | ||
validationError = err; | ||
compilationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
let errors; | ||
if (compiledValidator) { | ||
if (typeof this.localizer === "function") { | ||
this.localizer(compiledValidator.errors); | ||
} | ||
errors = compiledValidator.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
compiledValidator.errors = null; | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
validationError: compilationError | ||
}; | ||
@@ -313,13 +330,11 @@ } | ||
// Include form data with undefined values, which is required for validation. | ||
const rootSchema = schema; | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, rootSchema, true); | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, schema, true); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
validationError: invalidSchemaError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errors = [...errors, { | ||
stack: validationError.message | ||
stack: invalidSchemaError.message | ||
}]; | ||
@@ -331,9 +346,7 @@ } | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
$schema: { | ||
__errors: [validationError.message] | ||
} | ||
$schema: { | ||
__errors: [invalidSchemaError.message] | ||
} | ||
@@ -389,7 +402,8 @@ }; | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData- - The form data to validate | ||
* @param schema - The schema against which to validate the form data | ||
* @param formData - The form data to validate | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema["$id"] ?? ROOT_SCHEMA_PREFIX; | ||
try { | ||
@@ -400,9 +414,22 @@ // add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// that lives in the rootSchema but not in the schema in question. | ||
const result = this.ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX).validate(this.withIdRefPrefix(schema), formData); | ||
if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
const schemaWithIdRefPrefix = this.withIdRefPrefix(schema); | ||
let compiledValidator; | ||
if (schemaWithIdRefPrefix["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix["$id"]); | ||
} | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schemaWithIdRefPrefix); | ||
} | ||
const result = compiledValidator(formData); | ||
return result; | ||
} catch (e) { | ||
console.warn("Error encountered compiling schema:", e); | ||
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(ROOT_SCHEMA_PREFIX); | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -409,0 +436,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("lodash/toPath"),e=require("lodash/isObject"),t=require("lodash/clone"),a=require("@rjsf/utils"),s=require("ajv"),o=require("ajv-formats");function i(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var n=i(r),c=i(e),d=i(t),l=i(s),h=i(o);const u={allErrors:!0,multipleOfPrecision:8},f=/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,m=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;class v{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:a,ajvOptionsOverrides:s,ajvFormatOptions:o,AjvClass:i}=r;this.ajv=function(r,e,t,a,s){void 0===t&&(t={}),void 0===s&&(s=l.default);const o=new s({...u,...t});return"boolean"!=typeof a&&h.default(o,a),o.addFormat("data-url",m),o.addFormat("color",f),Array.isArray(r)&&o.addMetaSchema(r),c.default(e)&&Object.keys(e).forEach((r=>{o.addFormat(r,e[r])})),o}(t,a,s,o,i),this.localizer=e}toErrorSchema(r){return r.length?r.reduce(((r,e)=>{const{property:t,message:a}=e,s=n.default(t);let o=r;s.length>0&&""===s[0]&&s.splice(0,1);for(const r of s.slice(0))r in o||(o[r]={}),o=o[r];return Array.isArray(o.__errors)?o.__errors=o.__errors.concat(a):a&&(o.__errors=[a]),r}),{}):{}}toErrorList(r,e){if(void 0===e&&(e=[]),!r)return[];let t=[];return a.ERRORS_KEY in r&&(t=t.concat(r.__errors.map((r=>{const t=`.${e.join(".")}`;return{property:t,message:r,stack:`${t} ${r}`}})))),Object.keys(r).reduce(((t,s)=>(s!==a.ERRORS_KEY&&(t=t.concat(this.toErrorList(r[s],[...e,s]))),t)),t)}createErrorHandler(r){const e={__errors:[],addError(r){this.__errors.push(r)}};if(Array.isArray(r))return r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),e);if(c.default(r)){const t=r;return Object.keys(t).reduce(((r,e)=>({...r,[e]:this.createErrorHandler(t[e])})),e)}return e}unwrapErrorHandler(r){return Object.keys(r).reduce(((e,t)=>"addError"===t?e:t===a.ERRORS_KEY?{...e,[t]:r[t]}:{...e,[t]:this.unwrapErrorHandler(r[t])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),r.map((r=>{const{instancePath:e,keyword:t,message:a,params:s,schemaPath:o}=r,i=e.replace(/\//g,".");return{name:t,property:i,message:a,params:s,stack:`${i} ${a}`.trim(),schemaPath:o}}))}rawValidation(r,e){let t;try{this.ajv.validate(r,e)}catch(r){t=r}"function"==typeof this.localizer&&this.localizer(this.ajv.errors);const a=this.ajv.errors||void 0;return this.ajv.errors=null,{errors:a,validationError:t}}validateFormData(r,e,t,s){const o=a.getDefaultFormState(this,e,r,e,!0),i=this.rawValidation(e,o),{validationError:n}=i;let c=this.transformRJSFValidationErrors(i.errors);const d=n&&n.message&&n.message.includes("no schema with key or ref ");d&&(c=[...c,{stack:n.message}]),"function"==typeof s&&(c=s(c));let l=this.toErrorSchema(c);if(d&&(l={...l,$schema:{__errors:[n.message]}}),"function"!=typeof t)return{errors:c,errorSchema:l};const h=t(o,this.createErrorHandler(o)),u=this.unwrapErrorHandler(h);return a.mergeValidationData(this,{errors:c,errorSchema:l},u)}withIdRefPrefixObject(r){for(const e in r){const t=r[e];r[e]=e===a.REF_KEY&&"string"==typeof t&&t.startsWith("#")?"__rjsf_rootSchema"+t:this.withIdRefPrefix(t)}return r}withIdRefPrefixArray(r){for(let e=0;e<r.length;e++)r[e]=this.withIdRefPrefix(r[e]);return r}isValid(r,e,t){try{return this.ajv.addSchema(t,"__rjsf_rootSchema").validate(this.withIdRefPrefix(r),e)}catch(r){return!1}finally{this.ajv.removeSchema("__rjsf_rootSchema")}}withIdRefPrefix(r){return Array.isArray(r)?this.withIdRefPrefixArray([...r]):c.default(r)?this.withIdRefPrefixObject(d.default(r)):r}}function y(r,e){return void 0===r&&(r={}),new v(r,e)}var p=y();exports.customizeValidator=y,exports.default=p; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("lodash/toPath"),e=require("lodash/isObject"),t=require("lodash/clone"),a=require("@rjsf/utils"),o=require("ajv8"),s=require("ajv-formats");function i(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var n=i(r),d=i(e),c=i(t),l=i(o),h=i(s);const u={allErrors:!0,multipleOfPrecision:8,strict:!1},f=/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,m=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;class v{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:o,ajvOptionsOverrides:s,ajvFormatOptions:i,AjvClass:n}=r;this.ajv=function(r,e,t,o,s){void 0===t&&(t={}),void 0===s&&(s=l.default);const i=new s({...u,...t});return o?h.default(i,o):!1!==o&&h.default(i),i.addFormat("data-url",m),i.addFormat("color",f),i.addKeyword(a.ADDITIONAL_PROPERTY_FLAG),i.addKeyword(a.RJSF_ADDITONAL_PROPERTIES_FLAG),Array.isArray(r)&&i.addMetaSchema(r),d.default(e)&&Object.keys(e).forEach((r=>{i.addFormat(r,e[r])})),i}(t,o,s,i,n),this.localizer=e}toErrorSchema(r){return r.length?r.reduce(((r,e)=>{const{property:t,message:a}=e,o=n.default(t);let s=r;o.length>0&&""===o[0]&&o.splice(0,1);for(const r of o.slice(0))r in s||(s[r]={}),s=s[r];return Array.isArray(s.__errors)?s.__errors=s.__errors.concat(a):a&&(s.__errors=[a]),r}),{}):{}}toErrorList(r,e){if(void 0===e&&(e=[]),!r)return[];let t=[];return a.ERRORS_KEY in r&&(t=t.concat(r.__errors.map((r=>{const t=`.${e.join(".")}`;return{property:t,message:r,stack:`${t} ${r}`}})))),Object.keys(r).reduce(((t,o)=>(o!==a.ERRORS_KEY&&(t=t.concat(this.toErrorList(r[o],[...e,o]))),t)),t)}createErrorHandler(r){const e={__errors:[],addError(r){this.__errors.push(r)}};if(Array.isArray(r))return r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),e);if(d.default(r)){const t=r;return Object.keys(t).reduce(((r,e)=>({...r,[e]:this.createErrorHandler(t[e])})),e)}return e}unwrapErrorHandler(r){return Object.keys(r).reduce(((e,t)=>"addError"===t?e:t===a.ERRORS_KEY?{...e,[t]:r[t]}:{...e,[t]:this.unwrapErrorHandler(r[t])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),r.map((r=>{const{instancePath:e,keyword:t,message:a,params:o,schemaPath:s}=r,i=e.replace(/\//g,".");return{name:t,property:i,message:a,params:o,stack:`${i} ${a}`.trim(),schemaPath:s}}))}rawValidation(r,e){let t,a,o;r.$id&&(a=this.ajv.getSchema(r.$id));try{void 0===a&&(a=this.ajv.compile(r)),a(e)}catch(r){t=r}return a&&("function"==typeof this.localizer&&this.localizer(a.errors),o=a.errors||void 0,a.errors=null),{errors:o,validationError:t}}validateFormData(r,e,t,o){const s=a.getDefaultFormState(this,e,r,e,!0),i=this.rawValidation(e,s),{validationError:n}=i;let d=this.transformRJSFValidationErrors(i.errors);n&&(d=[...d,{stack:n.message}]),"function"==typeof o&&(d=o(d));let c=this.toErrorSchema(d);if(n&&(c={...c,$schema:{__errors:[n.message]}}),"function"!=typeof t)return{errors:d,errorSchema:c};const l=t(s,this.createErrorHandler(s)),h=this.unwrapErrorHandler(l);return a.mergeValidationData(this,{errors:d,errorSchema:c},h)}withIdRefPrefixObject(r){for(const e in r){const t=r[e];r[e]=e===a.REF_KEY&&"string"==typeof t&&t.startsWith("#")?"__rjsf_rootSchema"+t:this.withIdRefPrefix(t)}return r}withIdRefPrefixArray(r){for(let e=0;e<r.length;e++)r[e]=this.withIdRefPrefix(r[e]);return r}isValid(r,e,t){const a=t.$id??"__rjsf_rootSchema";try{void 0===this.ajv.getSchema(a)&&this.ajv.addSchema(t,a);const o=this.withIdRefPrefix(r);let s;return o.$id&&(s=this.ajv.getSchema(o.$id)),void 0===s&&(s=this.ajv.compile(o)),s(e)}catch(r){return console.warn("Error encountered compiling schema:",r),!1}finally{this.ajv.removeSchema(a)}}withIdRefPrefix(r){return Array.isArray(r)?this.withIdRefPrefixArray([...r]):d.default(r)?this.withIdRefPrefixObject(c.default(r)):r}}function p(r,e){return void 0===r&&(r={}),new v(r,e)}var _=p();exports.customizeValidator=p,exports.default=_; | ||
//# sourceMappingURL=validator-ajv8.cjs.production.min.js.map |
import toPath from 'lodash-es/toPath'; | ||
import isObject from 'lodash-es/isObject'; | ||
import clone from 'lodash-es/clone'; | ||
import { ERRORS_KEY, getDefaultFormState, mergeValidationData, REF_KEY } from '@rjsf/utils'; | ||
import Ajv from 'ajv'; | ||
import { ADDITIONAL_PROPERTY_FLAG, RJSF_ADDITONAL_PROPERTIES_FLAG, ERRORS_KEY, getDefaultFormState, mergeValidationData, REF_KEY } from '@rjsf/utils'; | ||
import Ajv from 'ajv8'; | ||
import addFormats from 'ajv-formats'; | ||
@@ -10,3 +10,4 @@ | ||
allErrors: true, | ||
multipleOfPrecision: 8 | ||
multipleOfPrecision: 8, | ||
strict: false | ||
}; | ||
@@ -28,2 +29,3 @@ const COLOR_FORMAT_REGEX = /^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/; | ||
* @param [ajvFormatOptions] - The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it | ||
* @param [AjvClass] - The `Ajv` class to use when creating the validator instance | ||
*/ | ||
@@ -41,4 +43,6 @@ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
if (ajvFormatOptions) { | ||
addFormats(ajv, ajvFormatOptions); | ||
} else if (ajvFormatOptions !== false) { | ||
addFormats(ajv); | ||
} | ||
@@ -48,2 +52,5 @@ // add custom formats | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// Add RJSF-specific additional properties keywords so Ajv doesn't report errors if strict is enabled. | ||
ajv.addKeyword(ADDITIONAL_PROPERTY_FLAG); | ||
ajv.addKeyword(RJSF_ADDITONAL_PROPERTIES_FLAG); | ||
// add more schemas to validate against | ||
@@ -270,17 +277,27 @@ if (Array.isArray(additionalMetaSchemas)) { | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
let compilationError = undefined; | ||
let compiledValidator; | ||
if (schema["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schema["$id"]); | ||
} | ||
try { | ||
this.ajv.validate(schema, formData); | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schema); | ||
} | ||
compiledValidator(formData); | ||
} catch (err) { | ||
validationError = err; | ||
compilationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
let errors; | ||
if (compiledValidator) { | ||
if (typeof this.localizer === "function") { | ||
this.localizer(compiledValidator.errors); | ||
} | ||
errors = compiledValidator.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
compiledValidator.errors = null; | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
validationError: compilationError | ||
}; | ||
@@ -300,13 +317,11 @@ } | ||
// Include form data with undefined values, which is required for validation. | ||
const rootSchema = schema; | ||
const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true); | ||
const newFormData = getDefaultFormState(this, schema, formData, schema, true); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
validationError: invalidSchemaError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errors = [...errors, { | ||
stack: validationError.message | ||
stack: invalidSchemaError.message | ||
}]; | ||
@@ -318,9 +333,7 @@ } | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
$schema: { | ||
__errors: [validationError.message] | ||
} | ||
$schema: { | ||
__errors: [invalidSchemaError.message] | ||
} | ||
@@ -376,7 +389,8 @@ }; | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData- - The form data to validate | ||
* @param schema - The schema against which to validate the form data | ||
* @param formData - The form data to validate | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema["$id"] ?? ROOT_SCHEMA_PREFIX; | ||
try { | ||
@@ -387,9 +401,22 @@ // add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// that lives in the rootSchema but not in the schema in question. | ||
const result = this.ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX).validate(this.withIdRefPrefix(schema), formData); | ||
if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
const schemaWithIdRefPrefix = this.withIdRefPrefix(schema); | ||
let compiledValidator; | ||
if (schemaWithIdRefPrefix["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix["$id"]); | ||
} | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schemaWithIdRefPrefix); | ||
} | ||
const result = compiledValidator(formData); | ||
return result; | ||
} catch (e) { | ||
console.warn("Error encountered compiling schema:", e); | ||
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(ROOT_SCHEMA_PREFIX); | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -396,0 +423,0 @@ } |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash-es/toPath'), require('lodash-es/isObject'), require('lodash-es/clone'), require('@rjsf/utils'), require('ajv'), require('ajv-formats')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'lodash-es/toPath', 'lodash-es/isObject', 'lodash-es/clone', '@rjsf/utils', 'ajv', 'ajv-formats'], factory) : | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash-es/toPath'), require('lodash-es/isObject'), require('lodash-es/clone'), require('@rjsf/utils'), require('ajv8'), require('ajv-formats')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'lodash-es/toPath', 'lodash-es/isObject', 'lodash-es/clone', '@rjsf/utils', 'ajv8', 'ajv-formats'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/validator-ajv8"] = {}, global.toPath, global.isObject, global.clone, global.utils, global.Ajv, global.addFormats)); | ||
@@ -17,3 +17,4 @@ })(this, (function (exports, toPath, isObject, clone, utils, Ajv, addFormats) { 'use strict'; | ||
allErrors: true, | ||
multipleOfPrecision: 8 | ||
multipleOfPrecision: 8, | ||
strict: false | ||
}; | ||
@@ -35,2 +36,3 @@ const COLOR_FORMAT_REGEX = /^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/; | ||
* @param [ajvFormatOptions] - The `ajv-format` options to use when adding formats to `ajv`; pass `false` to disable it | ||
* @param [AjvClass] - The `Ajv` class to use when creating the validator instance | ||
*/ | ||
@@ -48,4 +50,6 @@ function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
if (ajvFormatOptions) { | ||
addFormats__default["default"](ajv, ajvFormatOptions); | ||
} else if (ajvFormatOptions !== false) { | ||
addFormats__default["default"](ajv); | ||
} | ||
@@ -55,2 +59,5 @@ // add custom formats | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// Add RJSF-specific additional properties keywords so Ajv doesn't report errors if strict is enabled. | ||
ajv.addKeyword(utils.ADDITIONAL_PROPERTY_FLAG); | ||
ajv.addKeyword(utils.RJSF_ADDITONAL_PROPERTIES_FLAG); | ||
// add more schemas to validate against | ||
@@ -277,17 +284,27 @@ if (Array.isArray(additionalMetaSchemas)) { | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
let compilationError = undefined; | ||
let compiledValidator; | ||
if (schema["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schema["$id"]); | ||
} | ||
try { | ||
this.ajv.validate(schema, formData); | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schema); | ||
} | ||
compiledValidator(formData); | ||
} catch (err) { | ||
validationError = err; | ||
compilationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
let errors; | ||
if (compiledValidator) { | ||
if (typeof this.localizer === "function") { | ||
this.localizer(compiledValidator.errors); | ||
} | ||
errors = compiledValidator.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
compiledValidator.errors = null; | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
validationError: compilationError | ||
}; | ||
@@ -307,13 +324,11 @@ } | ||
// Include form data with undefined values, which is required for validation. | ||
const rootSchema = schema; | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, rootSchema, true); | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, schema, true); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
validationError: invalidSchemaError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errors = [...errors, { | ||
stack: validationError.message | ||
stack: invalidSchemaError.message | ||
}]; | ||
@@ -325,9 +340,7 @@ } | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
if (invalidSchemaError) { | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
$schema: { | ||
__errors: [validationError.message] | ||
} | ||
$schema: { | ||
__errors: [invalidSchemaError.message] | ||
} | ||
@@ -383,7 +396,8 @@ }; | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData- - The form data to validate | ||
* @param schema - The schema against which to validate the form data | ||
* @param formData - The form data to validate | ||
* @param rootSchema - The root schema used to provide $ref resolutions | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
const rootSchemaId = rootSchema["$id"] ?? ROOT_SCHEMA_PREFIX; | ||
try { | ||
@@ -394,9 +408,22 @@ // add the rootSchema ROOT_SCHEMA_PREFIX as id. | ||
// that lives in the rootSchema but not in the schema in question. | ||
const result = this.ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX).validate(this.withIdRefPrefix(schema), formData); | ||
if (this.ajv.getSchema(rootSchemaId) === undefined) { | ||
this.ajv.addSchema(rootSchema, rootSchemaId); | ||
} | ||
const schemaWithIdRefPrefix = this.withIdRefPrefix(schema); | ||
let compiledValidator; | ||
if (schemaWithIdRefPrefix["$id"]) { | ||
compiledValidator = this.ajv.getSchema(schemaWithIdRefPrefix["$id"]); | ||
} | ||
if (compiledValidator === undefined) { | ||
compiledValidator = this.ajv.compile(schemaWithIdRefPrefix); | ||
} | ||
const result = compiledValidator(formData); | ||
return result; | ||
} catch (e) { | ||
console.warn("Error encountered compiling schema:", e); | ||
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(ROOT_SCHEMA_PREFIX); | ||
this.ajv.removeSchema(rootSchemaId); | ||
} | ||
@@ -403,0 +430,0 @@ } |
@@ -1,2 +0,2 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("lodash-es/toPath"),require("lodash-es/isObject"),require("lodash-es/clone"),require("@rjsf/utils"),require("ajv"),require("ajv-formats")):"function"==typeof define&&define.amd?define(["exports","lodash-es/toPath","lodash-es/isObject","lodash-es/clone","@rjsf/utils","ajv","ajv-formats"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self)["@rjsf/validator-ajv8"]={},r.toPath,r.isObject,r.clone,r.utils,r.Ajv,r.addFormats)}(this,(function(r,e,t,a,s,o,i){"use strict";function n(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var d=n(e),l=n(t),c=n(a),u=n(o),h=n(i);const f={allErrors:!0,multipleOfPrecision:8},m=/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,v=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/,y="__rjsf_rootSchema";class p{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:a,ajvOptionsOverrides:s,ajvFormatOptions:o,AjvClass:i}=r;this.ajv=function(r,e,t,a,s){void 0===t&&(t={}),void 0===s&&(s=u.default);const o=new s({...f,...t});return"boolean"!=typeof a&&h.default(o,a),o.addFormat("data-url",v),o.addFormat("color",m),Array.isArray(r)&&o.addMetaSchema(r),l.default(e)&&Object.keys(e).forEach((r=>{o.addFormat(r,e[r])})),o}(t,a,s,o,i),this.localizer=e}toErrorSchema(r){return r.length?r.reduce(((r,e)=>{const{property:t,message:a}=e,s=d.default(t);let o=r;s.length>0&&""===s[0]&&s.splice(0,1);for(const r of s.slice(0))r in o||(o[r]={}),o=o[r];return Array.isArray(o.__errors)?o.__errors=o.__errors.concat(a):a&&(o.__errors=[a]),r}),{}):{}}toErrorList(r,e){if(void 0===e&&(e=[]),!r)return[];let t=[];return s.ERRORS_KEY in r&&(t=t.concat(r.__errors.map((r=>{const t=`.${e.join(".")}`;return{property:t,message:r,stack:`${t} ${r}`}})))),Object.keys(r).reduce(((t,a)=>(a!==s.ERRORS_KEY&&(t=t.concat(this.toErrorList(r[a],[...e,a]))),t)),t)}createErrorHandler(r){const e={__errors:[],addError(r){this.__errors.push(r)}};if(Array.isArray(r))return r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),e);if(l.default(r)){const t=r;return Object.keys(t).reduce(((r,e)=>({...r,[e]:this.createErrorHandler(t[e])})),e)}return e}unwrapErrorHandler(r){return Object.keys(r).reduce(((e,t)=>"addError"===t?e:t===s.ERRORS_KEY?{...e,[t]:r[t]}:{...e,[t]:this.unwrapErrorHandler(r[t])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),r.map((r=>{const{instancePath:e,keyword:t,message:a,params:s,schemaPath:o}=r,i=e.replace(/\//g,".");return{name:t,property:i,message:a,params:s,stack:`${i} ${a}`.trim(),schemaPath:o}}))}rawValidation(r,e){let t;try{this.ajv.validate(r,e)}catch(r){t=r}"function"==typeof this.localizer&&this.localizer(this.ajv.errors);const a=this.ajv.errors||void 0;return this.ajv.errors=null,{errors:a,validationError:t}}validateFormData(r,e,t,a){const o=s.getDefaultFormState(this,e,r,e,!0),i=this.rawValidation(e,o),{validationError:n}=i;let d=this.transformRJSFValidationErrors(i.errors);const l=n&&n.message&&n.message.includes("no schema with key or ref ");l&&(d=[...d,{stack:n.message}]),"function"==typeof a&&(d=a(d));let c=this.toErrorSchema(d);if(l&&(c={...c,$schema:{__errors:[n.message]}}),"function"!=typeof t)return{errors:d,errorSchema:c};const u=t(o,this.createErrorHandler(o)),h=this.unwrapErrorHandler(u);return s.mergeValidationData(this,{errors:d,errorSchema:c},h)}withIdRefPrefixObject(r){for(const e in r){const t=r[e];r[e]=e===s.REF_KEY&&"string"==typeof t&&t.startsWith("#")?y+t:this.withIdRefPrefix(t)}return r}withIdRefPrefixArray(r){for(let e=0;e<r.length;e++)r[e]=this.withIdRefPrefix(r[e]);return r}isValid(r,e,t){try{return this.ajv.addSchema(t,y).validate(this.withIdRefPrefix(r),e)}catch(r){return!1}finally{this.ajv.removeSchema(y)}}withIdRefPrefix(r){return Array.isArray(r)?this.withIdRefPrefixArray([...r]):l.default(r)?this.withIdRefPrefixObject(c.default(r)):r}}function j(r,e){return void 0===r&&(r={}),new p(r,e)}var b=j();r.customizeValidator=j,r.default=b,Object.defineProperty(r,"__esModule",{value:!0})})); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("lodash-es/toPath"),require("lodash-es/isObject"),require("lodash-es/clone"),require("@rjsf/utils"),require("ajv8"),require("ajv-formats")):"function"==typeof define&&define.amd?define(["exports","lodash-es/toPath","lodash-es/isObject","lodash-es/clone","@rjsf/utils","ajv8","ajv-formats"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self)["@rjsf/validator-ajv8"]={},r.toPath,r.isObject,r.clone,r.utils,r.Ajv,r.addFormats)}(this,(function(r,e,t,a,o,s,i){"use strict";function n(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var d=n(e),c=n(t),l=n(a),h=n(s),u=n(i);const f={allErrors:!0,multipleOfPrecision:8,strict:!1},m=/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,v=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/,p="__rjsf_rootSchema";class y{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:a,ajvOptionsOverrides:s,ajvFormatOptions:i,AjvClass:n}=r;this.ajv=function(r,e,t,a,s){void 0===t&&(t={}),void 0===s&&(s=h.default);const i=new s({...f,...t});return a?u.default(i,a):!1!==a&&u.default(i),i.addFormat("data-url",v),i.addFormat("color",m),i.addKeyword(o.ADDITIONAL_PROPERTY_FLAG),i.addKeyword(o.RJSF_ADDITONAL_PROPERTIES_FLAG),Array.isArray(r)&&i.addMetaSchema(r),c.default(e)&&Object.keys(e).forEach((r=>{i.addFormat(r,e[r])})),i}(t,a,s,i,n),this.localizer=e}toErrorSchema(r){return r.length?r.reduce(((r,e)=>{const{property:t,message:a}=e,o=d.default(t);let s=r;o.length>0&&""===o[0]&&o.splice(0,1);for(const r of o.slice(0))r in s||(s[r]={}),s=s[r];return Array.isArray(s.__errors)?s.__errors=s.__errors.concat(a):a&&(s.__errors=[a]),r}),{}):{}}toErrorList(r,e){if(void 0===e&&(e=[]),!r)return[];let t=[];return o.ERRORS_KEY in r&&(t=t.concat(r.__errors.map((r=>{const t=`.${e.join(".")}`;return{property:t,message:r,stack:`${t} ${r}`}})))),Object.keys(r).reduce(((t,a)=>(a!==o.ERRORS_KEY&&(t=t.concat(this.toErrorList(r[a],[...e,a]))),t)),t)}createErrorHandler(r){const e={__errors:[],addError(r){this.__errors.push(r)}};if(Array.isArray(r))return r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),e);if(c.default(r)){const t=r;return Object.keys(t).reduce(((r,e)=>({...r,[e]:this.createErrorHandler(t[e])})),e)}return e}unwrapErrorHandler(r){return Object.keys(r).reduce(((e,t)=>"addError"===t?e:t===o.ERRORS_KEY?{...e,[t]:r[t]}:{...e,[t]:this.unwrapErrorHandler(r[t])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),r.map((r=>{const{instancePath:e,keyword:t,message:a,params:o,schemaPath:s}=r,i=e.replace(/\//g,".");return{name:t,property:i,message:a,params:o,stack:`${i} ${a}`.trim(),schemaPath:s}}))}rawValidation(r,e){let t,a,o;r.$id&&(a=this.ajv.getSchema(r.$id));try{void 0===a&&(a=this.ajv.compile(r)),a(e)}catch(r){t=r}return a&&("function"==typeof this.localizer&&this.localizer(a.errors),o=a.errors||void 0,a.errors=null),{errors:o,validationError:t}}validateFormData(r,e,t,a){const s=o.getDefaultFormState(this,e,r,e,!0),i=this.rawValidation(e,s),{validationError:n}=i;let d=this.transformRJSFValidationErrors(i.errors);n&&(d=[...d,{stack:n.message}]),"function"==typeof a&&(d=a(d));let c=this.toErrorSchema(d);if(n&&(c={...c,$schema:{__errors:[n.message]}}),"function"!=typeof t)return{errors:d,errorSchema:c};const l=t(s,this.createErrorHandler(s)),h=this.unwrapErrorHandler(l);return o.mergeValidationData(this,{errors:d,errorSchema:c},h)}withIdRefPrefixObject(r){for(const e in r){const t=r[e];r[e]=e===o.REF_KEY&&"string"==typeof t&&t.startsWith("#")?p+t:this.withIdRefPrefix(t)}return r}withIdRefPrefixArray(r){for(let e=0;e<r.length;e++)r[e]=this.withIdRefPrefix(r[e]);return r}isValid(r,e,t){const a=t.$id??p;try{void 0===this.ajv.getSchema(a)&&this.ajv.addSchema(t,a);const o=this.withIdRefPrefix(r);let s;return o.$id&&(s=this.ajv.getSchema(o.$id)),void 0===s&&(s=this.ajv.compile(o)),s(e)}catch(r){return console.warn("Error encountered compiling schema:",r),!1}finally{this.ajv.removeSchema(a)}}withIdRefPrefix(r){return Array.isArray(r)?this.withIdRefPrefixArray([...r]):c.default(r)?this.withIdRefPrefixObject(l.default(r)):r}}function j(r,e){return void 0===r&&(r={}),new y(r,e)}var E=j();r.customizeValidator=j,r.default=E,Object.defineProperty(r,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=validator-ajv8.umd.production.min.js.map |
{ | ||
"name": "@rjsf/validator-ajv8", | ||
"version": "5.0.0-beta.12", | ||
"version": "5.0.0-beta.13", | ||
"main": "dist/index.js", | ||
@@ -30,4 +30,4 @@ "module": "dist/validator-ajv8.esm.js", | ||
"dependencies": { | ||
"ajv": "^8.11.0", | ||
"ajv-formats": "^2.1.1", | ||
"ajv8": "npm:ajv@^8.11.0", | ||
"lodash": "^4.17.15", | ||
@@ -37,3 +37,3 @@ "lodash-es": "^4.17.15" | ||
"peerDependencies": { | ||
"@rjsf/utils": "^5.0.0-beta.1" | ||
"@rjsf/utils": "^5.0.0-beta.12" | ||
}, | ||
@@ -47,3 +47,3 @@ "devDependencies": { | ||
"@babel/preset-react": "^7.18.6", | ||
"@rjsf/utils": "^5.0.0-beta.12", | ||
"@rjsf/utils": "^5.0.0-beta.13", | ||
"@types/jest-expect-message": "^1.1.0", | ||
@@ -78,3 +78,3 @@ "@types/json-schema": "^7.0.9", | ||
"license": "Apache-2.0", | ||
"gitHead": "d0068284cf667418ea236fd4f5c4708f51740961" | ||
"gitHead": "c06ccc2d11f998a3df74d3104e768f3dfeb3212d" | ||
} |
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
Sorry, the diff of this file is not supported yet
227960
1418
+ Addedajv8@npm:ajv@^8.11.0
- Removedajv@^8.11.0