@rjsf/validator-ajv8
Advanced tools
Comparing version 5.0.0-beta.11 to 5.0.0-beta.12
import * as _rjsf_utils from '@rjsf/utils'; | ||
import { ValidatorType } from '@rjsf/utils'; | ||
import { Options, ErrorObject } from 'ajv'; | ||
import { StrictRJSFSchema, RJSFSchema, ValidatorType } from '@rjsf/utils'; | ||
import Ajv, { Options, ErrorObject } from 'ajv'; | ||
import { FormatsPluginOptions } from 'ajv-formats'; | ||
@@ -19,2 +19,4 @@ | ||
ajvFormatOptions?: FormatsPluginOptions | false; | ||
/** The AJV class to construct */ | ||
AjvClass?: typeof Ajv; | ||
} | ||
@@ -31,6 +33,6 @@ /** The type describing a function that takes a list of Ajv `ErrorObject`s and localizes them | ||
*/ | ||
declare function customizeValidator<T = any>(options?: CustomValidatorOptionsType, localizer?: Localizer): ValidatorType<T>; | ||
declare function customizeValidator<T = any, S extends StrictRJSFSchema = RJSFSchema>(options?: CustomValidatorOptionsType, localizer?: Localizer): ValidatorType<T, S>; | ||
declare const _default: _rjsf_utils.ValidatorType<any>; | ||
declare const _default: _rjsf_utils.ValidatorType<any, _rjsf_utils.RJSFSchema>; | ||
export { CustomValidatorOptionsType, Localizer, customizeValidator, _default as default }; |
@@ -6,6 +6,7 @@ 'use strict'; | ||
var toPath = require('lodash/toPath'); | ||
var isObject = require('lodash/isObject'); | ||
var clone = require('lodash/clone'); | ||
var utils = require('@rjsf/utils'); | ||
var Ajv = require('ajv'); | ||
var addFormats = require('ajv-formats'); | ||
var isObject = require('lodash/isObject'); | ||
@@ -15,5 +16,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var toPath__default = /*#__PURE__*/_interopDefaultLegacy(toPath); | ||
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject); | ||
var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone); | ||
var Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv); | ||
var addFormats__default = /*#__PURE__*/_interopDefaultLegacy(addFormats); | ||
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject); | ||
@@ -40,25 +42,24 @@ const AJV_CONFIG = { | ||
*/ | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions) { | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
if (ajvOptionsOverrides === void 0) { | ||
ajvOptionsOverrides = {}; | ||
} | ||
const ajv = new Ajv__default["default"]({ ...AJV_CONFIG, | ||
if (AjvClass === void 0) { | ||
AjvClass = Ajv__default["default"]; | ||
} | ||
const ajv = new AjvClass({ | ||
...AJV_CONFIG, | ||
...ajvOptionsOverrides | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
addFormats__default["default"](ajv, ajvFormatOptions); | ||
} // add custom formats | ||
} | ||
// add custom formats | ||
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); // add more schemas to validate against | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// add more schemas to validate against | ||
if (Array.isArray(additionalMetaSchemas)) { | ||
ajv.addMetaSchema(additionalMetaSchemas); | ||
} // add more custom formats to validate against | ||
} | ||
// add more custom formats to validate against | ||
if (isObject__default["default"](customFormats)) { | ||
@@ -69,3 +70,2 @@ Object.keys(customFormats).forEach(formatName => { | ||
} | ||
return ajv; | ||
@@ -77,3 +77,2 @@ } | ||
*/ | ||
class AJV8Validator { | ||
@@ -102,5 +101,6 @@ /** The AJV instance to use for all validations | ||
ajvOptionsOverrides, | ||
ajvFormatOptions | ||
ajvFormatOptions, | ||
AjvClass | ||
} = options; | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions); | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass); | ||
this.localizer = localizer; | ||
@@ -127,4 +127,2 @@ } | ||
*/ | ||
toErrorSchema(errors) { | ||
@@ -134,3 +132,2 @@ if (!errors.length) { | ||
} | ||
return errors.reduce((errorSchema, error) => { | ||
@@ -142,9 +139,8 @@ const { | ||
const path = toPath__default["default"](property); | ||
let parent = errorSchema; // If the property is at the root (.level1) then toPath creates | ||
let parent = errorSchema; | ||
// If the property is at the root (.level1) then toPath creates | ||
// an empty array element at the first index. Remove it. | ||
if (path.length > 0 && path[0] === "") { | ||
path.splice(0, 1); | ||
} | ||
for (const segment of path.slice(0)) { | ||
@@ -154,6 +150,4 @@ if (!(segment in parent)) { | ||
} | ||
parent = parent[segment]; | ||
} | ||
if (Array.isArray(parent.__errors)) { | ||
@@ -169,3 +163,2 @@ // We store the list of errors for this node in a property named __errors | ||
} | ||
return errorSchema; | ||
@@ -179,4 +172,2 @@ }, {}); | ||
*/ | ||
toErrorList(errorSchema, fieldPath) { | ||
@@ -186,20 +177,16 @@ if (fieldPath === void 0) { | ||
} | ||
if (!errorSchema) { | ||
return []; | ||
} | ||
let errorList = []; | ||
if (utils.ERRORS_KEY in errorSchema) { | ||
errorList = errorList.concat(errorSchema.__errors.map(message => { | ||
const property = "." + fieldPath.join("."); | ||
const property = `.${fieldPath.join(".")}`; | ||
return { | ||
property, | ||
message, | ||
stack: property + " " + message | ||
stack: `${property} ${message}` | ||
}; | ||
})); | ||
} | ||
return Object.keys(errorSchema).reduce((acc, key) => { | ||
@@ -209,3 +196,2 @@ if (key !== utils.ERRORS_KEY) { | ||
} | ||
return acc; | ||
@@ -219,4 +205,2 @@ }, errorList); | ||
*/ | ||
createErrorHandler(formData) { | ||
@@ -228,13 +212,19 @@ const handler = { | ||
__errors: [], | ||
addError(message) { | ||
this.__errors.push(message); | ||
} | ||
}; | ||
if (utils.isObject(formData)) { | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
if (isObject__default["default"](formData)) { | ||
const formObject = formData; | ||
return Object.keys(formObject).reduce((acc, key) => { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(formObject[key]) | ||
@@ -244,11 +234,2 @@ }; | ||
} | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { ...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
return handler; | ||
@@ -261,4 +242,2 @@ } | ||
*/ | ||
unwrapErrorHandler(errorHandler) { | ||
@@ -269,8 +248,9 @@ return Object.keys(errorHandler).reduce((acc, key) => { | ||
} else if (key === utils.ERRORS_KEY) { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: errorHandler[key] | ||
}; | ||
} | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.unwrapErrorHandler(errorHandler[key]) | ||
@@ -286,4 +266,2 @@ }; | ||
*/ | ||
transformRJSFValidationErrors(errors) { | ||
@@ -293,7 +271,2 @@ if (errors === void 0) { | ||
} | ||
if (errors === null) { | ||
return []; | ||
} | ||
return errors.map(e => { | ||
@@ -307,4 +280,4 @@ const { | ||
} = e; | ||
const property = instancePath.replace(/\//g, "."); // put data in expected format | ||
const property = instancePath.replace(/\//g, "."); | ||
// put data in expected format | ||
return { | ||
@@ -315,3 +288,3 @@ name: keyword, | ||
params, | ||
stack: (property + " " + message).trim(), | ||
stack: `${property} ${message}`.trim(), | ||
schemaPath | ||
@@ -321,2 +294,26 @@ }; | ||
} | ||
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use | ||
* by the playground. Returns the `errors` from the validation | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData - The form data to validate | ||
*/ | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
try { | ||
this.ajv.validate(schema, formData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
}; | ||
} | ||
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives | ||
@@ -332,4 +329,2 @@ * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also | ||
*/ | ||
validateFormData(formData, schema, customValidate, transformErrors) { | ||
@@ -339,19 +334,8 @@ // Include form data with undefined values, which is required for validation. | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, rootSchema, true); | ||
let validationError = null; | ||
try { | ||
this.ajv.validate(schema, newFormData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
let errors = this.transformRJSFValidationErrors(this.ajv.errors); // Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
const noProperMetaSchema = validationError && validationError.message && typeof validationError.message === "string" && validationError.message.includes("no schema with key or ref "); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
@@ -362,11 +346,9 @@ errors = [...errors, { | ||
} | ||
if (typeof transformErrors === "function") { | ||
errors = transformErrors(errors); | ||
} | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
errorSchema = { ...errorSchema, | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
@@ -379,3 +361,2 @@ $schema: { | ||
} | ||
if (typeof customValidate !== "function") { | ||
@@ -387,3 +368,2 @@ return { | ||
} | ||
const errorHandler = customValidate(newFormData, this.createErrorHandler(newFormData)); | ||
@@ -402,4 +382,2 @@ const userErrorSchema = this.unwrapErrorHandler(errorHandler); | ||
*/ | ||
withIdRefPrefixObject(node) { | ||
@@ -409,3 +387,2 @@ for (const key in node) { | ||
const value = realObj[key]; | ||
if (key === utils.REF_KEY && typeof value === "string" && value.startsWith("#")) { | ||
@@ -417,3 +394,2 @@ realObj[key] = ROOT_SCHEMA_PREFIX + value; | ||
} | ||
return node; | ||
@@ -424,7 +400,5 @@ } | ||
* | ||
* @param nodeThe - list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @param node - The list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @private | ||
*/ | ||
withIdRefPrefixArray(node) { | ||
@@ -434,3 +408,2 @@ for (let i = 0; i < node.length; i++) { | ||
} | ||
return node; | ||
@@ -446,4 +419,2 @@ } | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
@@ -470,17 +441,11 @@ try { | ||
*/ | ||
withIdRefPrefix(schemaNode) { | ||
if (schemaNode.constructor === Object) { | ||
return this.withIdRefPrefixObject({ ...schemaNode | ||
}); | ||
} | ||
if (Array.isArray(schemaNode)) { | ||
return this.withIdRefPrefixArray([...schemaNode]); | ||
} | ||
if (isObject__default["default"](schemaNode)) { | ||
return this.withIdRefPrefixObject(clone__default["default"](schemaNode)); | ||
} | ||
return schemaNode; | ||
} | ||
} | ||
@@ -494,3 +459,2 @@ | ||
*/ | ||
function customizeValidator(options, localizer) { | ||
@@ -500,3 +464,2 @@ if (options === void 0) { | ||
} | ||
return new AJV8Validator(options, localizer); | ||
@@ -503,0 +466,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("lodash/toPath"),e=require("@rjsf/utils"),t=require("ajv"),a=require("ajv-formats"),s=require("lodash/isObject");function o(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var i=o(r),n=o(t),c=o(a),l=o(s);const d={allErrors:!0,multipleOfPrecision:8},h=/^(#?([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*\)))$/,u=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/;class f{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:a,ajvOptionsOverrides:s,ajvFormatOptions:o}=r;this.ajv=function(r,e,t,a){void 0===t&&(t={});const s=new n.default({...d,...t});return"boolean"!=typeof a&&c.default(s,a),s.addFormat("data-url",u),s.addFormat("color",h),Array.isArray(r)&&s.addMetaSchema(r),l.default(e)&&Object.keys(e).forEach((r=>{s.addFormat(r,e[r])})),s}(t,a,s,o),this.localizer=e}toErrorSchema(r){return r.length?r.reduce(((r,e)=>{const{property:t,message:a}=e,s=i.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,t){if(void 0===t&&(t=[]),!r)return[];let a=[];return e.ERRORS_KEY in r&&(a=a.concat(r.__errors.map((r=>{const e="."+t.join(".");return{property:e,message:r,stack:e+" "+r}})))),Object.keys(r).reduce(((a,s)=>(s!==e.ERRORS_KEY&&(a=a.concat(this.toErrorList(r[s],[...t,s]))),a)),a)}createErrorHandler(r){const t={__errors:[],addError(r){this.__errors.push(r)}};if(e.isObject(r)){const e=r;return Object.keys(e).reduce(((r,t)=>({...r,[t]:this.createErrorHandler(e[t])})),t)}return Array.isArray(r)?r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),t):t}unwrapErrorHandler(r){return Object.keys(r).reduce(((t,a)=>"addError"===a?t:a===e.ERRORS_KEY?{...t,[a]:r[a]}:{...t,[a]:this.unwrapErrorHandler(r[a])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),null===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}}))}validateFormData(r,t,a,s){const o=e.getDefaultFormState(this,t,r,t,!0);let i=null;try{this.ajv.validate(t,o)}catch(r){i=r}"function"==typeof this.localizer&&this.localizer(this.ajv.errors);let n=this.transformRJSFValidationErrors(this.ajv.errors);this.ajv.errors=null;const c=i&&i.message&&"string"==typeof i.message&&i.message.includes("no schema with key or ref ");c&&(n=[...n,{stack:i.message}]),"function"==typeof s&&(n=s(n));let l=this.toErrorSchema(n);if(c&&(l={...l,$schema:{__errors:[i.message]}}),"function"!=typeof a)return{errors:n,errorSchema:l};const d=a(o,this.createErrorHandler(o)),h=this.unwrapErrorHandler(d);return e.mergeValidationData(this,{errors:n,errorSchema:l},h)}withIdRefPrefixObject(r){for(const t in r){const a=r[t];r[t]=t===e.REF_KEY&&"string"==typeof a&&a.startsWith("#")?"__rjsf_rootSchema"+a:this.withIdRefPrefix(a)}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 r.constructor===Object?this.withIdRefPrefixObject({...r}):Array.isArray(r)?this.withIdRefPrefixArray([...r]):r}}function m(r,e){return void 0===r&&(r={}),new f(r,e)}var y=m();exports.customizeValidator=m,exports.default=y; | ||
"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; | ||
//# sourceMappingURL=validator-ajv8.cjs.production.min.js.map |
import toPath from 'lodash-es/toPath'; | ||
import { ERRORS_KEY, isObject as isObject$1, getDefaultFormState, mergeValidationData, REF_KEY } from '@rjsf/utils'; | ||
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 addFormats from 'ajv-formats'; | ||
import isObject from 'lodash-es/isObject'; | ||
@@ -27,25 +28,24 @@ const AJV_CONFIG = { | ||
*/ | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions) { | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
if (ajvOptionsOverrides === void 0) { | ||
ajvOptionsOverrides = {}; | ||
} | ||
const ajv = new Ajv({ ...AJV_CONFIG, | ||
if (AjvClass === void 0) { | ||
AjvClass = Ajv; | ||
} | ||
const ajv = new AjvClass({ | ||
...AJV_CONFIG, | ||
...ajvOptionsOverrides | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
addFormats(ajv, ajvFormatOptions); | ||
} // add custom formats | ||
} | ||
// add custom formats | ||
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); // add more schemas to validate against | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// add more schemas to validate against | ||
if (Array.isArray(additionalMetaSchemas)) { | ||
ajv.addMetaSchema(additionalMetaSchemas); | ||
} // add more custom formats to validate against | ||
} | ||
// add more custom formats to validate against | ||
if (isObject(customFormats)) { | ||
@@ -56,3 +56,2 @@ Object.keys(customFormats).forEach(formatName => { | ||
} | ||
return ajv; | ||
@@ -64,3 +63,2 @@ } | ||
*/ | ||
class AJV8Validator { | ||
@@ -89,5 +87,6 @@ /** The AJV instance to use for all validations | ||
ajvOptionsOverrides, | ||
ajvFormatOptions | ||
ajvFormatOptions, | ||
AjvClass | ||
} = options; | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions); | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass); | ||
this.localizer = localizer; | ||
@@ -114,4 +113,2 @@ } | ||
*/ | ||
toErrorSchema(errors) { | ||
@@ -121,3 +118,2 @@ if (!errors.length) { | ||
} | ||
return errors.reduce((errorSchema, error) => { | ||
@@ -129,9 +125,8 @@ const { | ||
const path = toPath(property); | ||
let parent = errorSchema; // If the property is at the root (.level1) then toPath creates | ||
let parent = errorSchema; | ||
// If the property is at the root (.level1) then toPath creates | ||
// an empty array element at the first index. Remove it. | ||
if (path.length > 0 && path[0] === "") { | ||
path.splice(0, 1); | ||
} | ||
for (const segment of path.slice(0)) { | ||
@@ -141,6 +136,4 @@ if (!(segment in parent)) { | ||
} | ||
parent = parent[segment]; | ||
} | ||
if (Array.isArray(parent.__errors)) { | ||
@@ -156,3 +149,2 @@ // We store the list of errors for this node in a property named __errors | ||
} | ||
return errorSchema; | ||
@@ -166,4 +158,2 @@ }, {}); | ||
*/ | ||
toErrorList(errorSchema, fieldPath) { | ||
@@ -173,20 +163,16 @@ if (fieldPath === void 0) { | ||
} | ||
if (!errorSchema) { | ||
return []; | ||
} | ||
let errorList = []; | ||
if (ERRORS_KEY in errorSchema) { | ||
errorList = errorList.concat(errorSchema.__errors.map(message => { | ||
const property = "." + fieldPath.join("."); | ||
const property = `.${fieldPath.join(".")}`; | ||
return { | ||
property, | ||
message, | ||
stack: property + " " + message | ||
stack: `${property} ${message}` | ||
}; | ||
})); | ||
} | ||
return Object.keys(errorSchema).reduce((acc, key) => { | ||
@@ -196,3 +182,2 @@ if (key !== ERRORS_KEY) { | ||
} | ||
return acc; | ||
@@ -206,4 +191,2 @@ }, errorList); | ||
*/ | ||
createErrorHandler(formData) { | ||
@@ -215,13 +198,19 @@ const handler = { | ||
__errors: [], | ||
addError(message) { | ||
this.__errors.push(message); | ||
} | ||
}; | ||
if (isObject$1(formData)) { | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
if (isObject(formData)) { | ||
const formObject = formData; | ||
return Object.keys(formObject).reduce((acc, key) => { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(formObject[key]) | ||
@@ -231,11 +220,2 @@ }; | ||
} | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { ...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
return handler; | ||
@@ -248,4 +228,2 @@ } | ||
*/ | ||
unwrapErrorHandler(errorHandler) { | ||
@@ -256,8 +234,9 @@ return Object.keys(errorHandler).reduce((acc, key) => { | ||
} else if (key === ERRORS_KEY) { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: errorHandler[key] | ||
}; | ||
} | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.unwrapErrorHandler(errorHandler[key]) | ||
@@ -273,4 +252,2 @@ }; | ||
*/ | ||
transformRJSFValidationErrors(errors) { | ||
@@ -280,7 +257,2 @@ if (errors === void 0) { | ||
} | ||
if (errors === null) { | ||
return []; | ||
} | ||
return errors.map(e => { | ||
@@ -294,4 +266,4 @@ const { | ||
} = e; | ||
const property = instancePath.replace(/\//g, "."); // put data in expected format | ||
const property = instancePath.replace(/\//g, "."); | ||
// put data in expected format | ||
return { | ||
@@ -302,3 +274,3 @@ name: keyword, | ||
params, | ||
stack: (property + " " + message).trim(), | ||
stack: `${property} ${message}`.trim(), | ||
schemaPath | ||
@@ -308,2 +280,26 @@ }; | ||
} | ||
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use | ||
* by the playground. Returns the `errors` from the validation | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData - The form data to validate | ||
*/ | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
try { | ||
this.ajv.validate(schema, formData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
}; | ||
} | ||
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives | ||
@@ -319,4 +315,2 @@ * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also | ||
*/ | ||
validateFormData(formData, schema, customValidate, transformErrors) { | ||
@@ -326,19 +320,8 @@ // Include form data with undefined values, which is required for validation. | ||
const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true); | ||
let validationError = null; | ||
try { | ||
this.ajv.validate(schema, newFormData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
let errors = this.transformRJSFValidationErrors(this.ajv.errors); // Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
const noProperMetaSchema = validationError && validationError.message && typeof validationError.message === "string" && validationError.message.includes("no schema with key or ref "); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
@@ -349,11 +332,9 @@ errors = [...errors, { | ||
} | ||
if (typeof transformErrors === "function") { | ||
errors = transformErrors(errors); | ||
} | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
errorSchema = { ...errorSchema, | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
@@ -366,3 +347,2 @@ $schema: { | ||
} | ||
if (typeof customValidate !== "function") { | ||
@@ -374,3 +354,2 @@ return { | ||
} | ||
const errorHandler = customValidate(newFormData, this.createErrorHandler(newFormData)); | ||
@@ -389,4 +368,2 @@ const userErrorSchema = this.unwrapErrorHandler(errorHandler); | ||
*/ | ||
withIdRefPrefixObject(node) { | ||
@@ -396,3 +373,2 @@ for (const key in node) { | ||
const value = realObj[key]; | ||
if (key === REF_KEY && typeof value === "string" && value.startsWith("#")) { | ||
@@ -404,3 +380,2 @@ realObj[key] = ROOT_SCHEMA_PREFIX + value; | ||
} | ||
return node; | ||
@@ -411,7 +386,5 @@ } | ||
* | ||
* @param nodeThe - list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @param node - The list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @private | ||
*/ | ||
withIdRefPrefixArray(node) { | ||
@@ -421,3 +394,2 @@ for (let i = 0; i < node.length; i++) { | ||
} | ||
return node; | ||
@@ -433,4 +405,2 @@ } | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
@@ -457,17 +427,11 @@ try { | ||
*/ | ||
withIdRefPrefix(schemaNode) { | ||
if (schemaNode.constructor === Object) { | ||
return this.withIdRefPrefixObject({ ...schemaNode | ||
}); | ||
} | ||
if (Array.isArray(schemaNode)) { | ||
return this.withIdRefPrefixArray([...schemaNode]); | ||
} | ||
if (isObject(schemaNode)) { | ||
return this.withIdRefPrefixObject(clone(schemaNode)); | ||
} | ||
return schemaNode; | ||
} | ||
} | ||
@@ -481,3 +445,2 @@ | ||
*/ | ||
function customizeValidator(options, localizer) { | ||
@@ -487,3 +450,2 @@ if (options === void 0) { | ||
} | ||
return new AJV8Validator(options, localizer); | ||
@@ -490,0 +452,0 @@ } |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash-es/toPath'), require('@rjsf/utils'), require('ajv'), require('ajv-formats'), require('lodash-es/isObject')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'lodash-es/toPath', '@rjsf/utils', 'ajv', 'ajv-formats', 'lodash-es/isObject'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/validator-ajv8"] = {}, global.toPath, global.utils, global.Ajv, global.addFormats, global.isObject)); | ||
})(this, (function (exports, toPath, utils, Ajv, addFormats, isObject) { 'use strict'; | ||
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) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/validator-ajv8"] = {}, global.toPath, global.isObject, global.clone, global.utils, global.Ajv, global.addFormats)); | ||
})(this, (function (exports, toPath, isObject, clone, utils, Ajv, addFormats) { 'use strict'; | ||
@@ -10,5 +10,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var toPath__default = /*#__PURE__*/_interopDefaultLegacy(toPath); | ||
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject); | ||
var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone); | ||
var Ajv__default = /*#__PURE__*/_interopDefaultLegacy(Ajv); | ||
var addFormats__default = /*#__PURE__*/_interopDefaultLegacy(addFormats); | ||
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject); | ||
@@ -35,25 +36,24 @@ const AJV_CONFIG = { | ||
*/ | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions) { | ||
function createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass) { | ||
if (ajvOptionsOverrides === void 0) { | ||
ajvOptionsOverrides = {}; | ||
} | ||
const ajv = new Ajv__default["default"]({ ...AJV_CONFIG, | ||
if (AjvClass === void 0) { | ||
AjvClass = Ajv__default["default"]; | ||
} | ||
const ajv = new AjvClass({ | ||
...AJV_CONFIG, | ||
...ajvOptionsOverrides | ||
}); | ||
if (typeof ajvFormatOptions !== "boolean") { | ||
addFormats__default["default"](ajv, ajvFormatOptions); | ||
} // add custom formats | ||
} | ||
// add custom formats | ||
ajv.addFormat("data-url", DATA_URL_FORMAT_REGEX); | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); // add more schemas to validate against | ||
ajv.addFormat("color", COLOR_FORMAT_REGEX); | ||
// add more schemas to validate against | ||
if (Array.isArray(additionalMetaSchemas)) { | ||
ajv.addMetaSchema(additionalMetaSchemas); | ||
} // add more custom formats to validate against | ||
} | ||
// add more custom formats to validate against | ||
if (isObject__default["default"](customFormats)) { | ||
@@ -64,3 +64,2 @@ Object.keys(customFormats).forEach(formatName => { | ||
} | ||
return ajv; | ||
@@ -72,3 +71,2 @@ } | ||
*/ | ||
class AJV8Validator { | ||
@@ -97,5 +95,6 @@ /** The AJV instance to use for all validations | ||
ajvOptionsOverrides, | ||
ajvFormatOptions | ||
ajvFormatOptions, | ||
AjvClass | ||
} = options; | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions); | ||
this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides, ajvFormatOptions, AjvClass); | ||
this.localizer = localizer; | ||
@@ -122,4 +121,2 @@ } | ||
*/ | ||
toErrorSchema(errors) { | ||
@@ -129,3 +126,2 @@ if (!errors.length) { | ||
} | ||
return errors.reduce((errorSchema, error) => { | ||
@@ -137,9 +133,8 @@ const { | ||
const path = toPath__default["default"](property); | ||
let parent = errorSchema; // If the property is at the root (.level1) then toPath creates | ||
let parent = errorSchema; | ||
// If the property is at the root (.level1) then toPath creates | ||
// an empty array element at the first index. Remove it. | ||
if (path.length > 0 && path[0] === "") { | ||
path.splice(0, 1); | ||
} | ||
for (const segment of path.slice(0)) { | ||
@@ -149,6 +144,4 @@ if (!(segment in parent)) { | ||
} | ||
parent = parent[segment]; | ||
} | ||
if (Array.isArray(parent.__errors)) { | ||
@@ -164,3 +157,2 @@ // We store the list of errors for this node in a property named __errors | ||
} | ||
return errorSchema; | ||
@@ -174,4 +166,2 @@ }, {}); | ||
*/ | ||
toErrorList(errorSchema, fieldPath) { | ||
@@ -181,20 +171,16 @@ if (fieldPath === void 0) { | ||
} | ||
if (!errorSchema) { | ||
return []; | ||
} | ||
let errorList = []; | ||
if (utils.ERRORS_KEY in errorSchema) { | ||
errorList = errorList.concat(errorSchema.__errors.map(message => { | ||
const property = "." + fieldPath.join("."); | ||
const property = `.${fieldPath.join(".")}`; | ||
return { | ||
property, | ||
message, | ||
stack: property + " " + message | ||
stack: `${property} ${message}` | ||
}; | ||
})); | ||
} | ||
return Object.keys(errorSchema).reduce((acc, key) => { | ||
@@ -204,3 +190,2 @@ if (key !== utils.ERRORS_KEY) { | ||
} | ||
return acc; | ||
@@ -214,4 +199,2 @@ }, errorList); | ||
*/ | ||
createErrorHandler(formData) { | ||
@@ -223,13 +206,19 @@ const handler = { | ||
__errors: [], | ||
addError(message) { | ||
this.__errors.push(message); | ||
} | ||
}; | ||
if (utils.isObject(formData)) { | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
if (isObject__default["default"](formData)) { | ||
const formObject = formData; | ||
return Object.keys(formObject).reduce((acc, key) => { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.createErrorHandler(formObject[key]) | ||
@@ -239,11 +228,2 @@ }; | ||
} | ||
if (Array.isArray(formData)) { | ||
return formData.reduce((acc, value, key) => { | ||
return { ...acc, | ||
[key]: this.createErrorHandler(value) | ||
}; | ||
}, handler); | ||
} | ||
return handler; | ||
@@ -256,4 +236,2 @@ } | ||
*/ | ||
unwrapErrorHandler(errorHandler) { | ||
@@ -264,8 +242,9 @@ return Object.keys(errorHandler).reduce((acc, key) => { | ||
} else if (key === utils.ERRORS_KEY) { | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: errorHandler[key] | ||
}; | ||
} | ||
return { ...acc, | ||
return { | ||
...acc, | ||
[key]: this.unwrapErrorHandler(errorHandler[key]) | ||
@@ -281,4 +260,2 @@ }; | ||
*/ | ||
transformRJSFValidationErrors(errors) { | ||
@@ -288,7 +265,2 @@ if (errors === void 0) { | ||
} | ||
if (errors === null) { | ||
return []; | ||
} | ||
return errors.map(e => { | ||
@@ -302,4 +274,4 @@ const { | ||
} = e; | ||
const property = instancePath.replace(/\//g, "."); // put data in expected format | ||
const property = instancePath.replace(/\//g, "."); | ||
// put data in expected format | ||
return { | ||
@@ -310,3 +282,3 @@ name: keyword, | ||
params, | ||
stack: (property + " " + message).trim(), | ||
stack: `${property} ${message}`.trim(), | ||
schemaPath | ||
@@ -316,2 +288,26 @@ }; | ||
} | ||
/** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use | ||
* by the playground. Returns the `errors` from the validation | ||
* | ||
* @param schema - The schema against which to validate the form data * @param schema | ||
* @param formData - The form data to validate | ||
*/ | ||
rawValidation(schema, formData) { | ||
let validationError = undefined; | ||
try { | ||
this.ajv.validate(schema, formData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
const errors = this.ajv.errors || undefined; | ||
// Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
return { | ||
errors: errors, | ||
validationError | ||
}; | ||
} | ||
/** This function processes the `formData` with an optional user contributed `customValidate` function, which receives | ||
@@ -327,4 +323,2 @@ * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also | ||
*/ | ||
validateFormData(formData, schema, customValidate, transformErrors) { | ||
@@ -334,19 +328,8 @@ // Include form data with undefined values, which is required for validation. | ||
const newFormData = utils.getDefaultFormState(this, schema, formData, rootSchema, true); | ||
let validationError = null; | ||
try { | ||
this.ajv.validate(schema, newFormData); | ||
} catch (err) { | ||
validationError = err; | ||
} | ||
if (typeof this.localizer === "function") { | ||
this.localizer(this.ajv.errors); | ||
} | ||
let errors = this.transformRJSFValidationErrors(this.ajv.errors); // Clear errors to prevent persistent errors, see #1104 | ||
this.ajv.errors = null; | ||
const noProperMetaSchema = validationError && validationError.message && typeof validationError.message === "string" && validationError.message.includes("no schema with key or ref "); | ||
const rawErrors = this.rawValidation(schema, newFormData); | ||
const { | ||
validationError | ||
} = rawErrors; | ||
let errors = this.transformRJSFValidationErrors(rawErrors.errors); | ||
const noProperMetaSchema = validationError && validationError.message && validationError.message.includes("no schema with key or ref "); | ||
if (noProperMetaSchema) { | ||
@@ -357,11 +340,9 @@ errors = [...errors, { | ||
} | ||
if (typeof transformErrors === "function") { | ||
errors = transformErrors(errors); | ||
} | ||
let errorSchema = this.toErrorSchema(errors); | ||
if (noProperMetaSchema) { | ||
errorSchema = { ...errorSchema, | ||
errorSchema = { | ||
...errorSchema, | ||
...{ | ||
@@ -374,3 +355,2 @@ $schema: { | ||
} | ||
if (typeof customValidate !== "function") { | ||
@@ -382,3 +362,2 @@ return { | ||
} | ||
const errorHandler = customValidate(newFormData, this.createErrorHandler(newFormData)); | ||
@@ -397,4 +376,2 @@ const userErrorSchema = this.unwrapErrorHandler(errorHandler); | ||
*/ | ||
withIdRefPrefixObject(node) { | ||
@@ -404,3 +381,2 @@ for (const key in node) { | ||
const value = realObj[key]; | ||
if (key === utils.REF_KEY && typeof value === "string" && value.startsWith("#")) { | ||
@@ -412,3 +388,2 @@ realObj[key] = ROOT_SCHEMA_PREFIX + value; | ||
} | ||
return node; | ||
@@ -419,7 +394,5 @@ } | ||
* | ||
* @param nodeThe - list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @param node - The list of object nodes to which a ROOT_SCHEMA_PREFIX is added when a REF_KEY is part of it | ||
* @private | ||
*/ | ||
withIdRefPrefixArray(node) { | ||
@@ -429,3 +402,2 @@ for (let i = 0; i < node.length; i++) { | ||
} | ||
return node; | ||
@@ -441,4 +413,2 @@ } | ||
*/ | ||
isValid(schema, formData, rootSchema) { | ||
@@ -465,17 +435,11 @@ try { | ||
*/ | ||
withIdRefPrefix(schemaNode) { | ||
if (schemaNode.constructor === Object) { | ||
return this.withIdRefPrefixObject({ ...schemaNode | ||
}); | ||
} | ||
if (Array.isArray(schemaNode)) { | ||
return this.withIdRefPrefixArray([...schemaNode]); | ||
} | ||
if (isObject__default["default"](schemaNode)) { | ||
return this.withIdRefPrefixObject(clone__default["default"](schemaNode)); | ||
} | ||
return schemaNode; | ||
} | ||
} | ||
@@ -489,3 +453,2 @@ | ||
*/ | ||
function customizeValidator(options, localizer) { | ||
@@ -495,3 +458,2 @@ if (options === void 0) { | ||
} | ||
return new AJV8Validator(options, localizer); | ||
@@ -498,0 +460,0 @@ } |
@@ -1,2 +0,2 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("lodash-es/toPath"),require("@rjsf/utils"),require("ajv"),require("ajv-formats"),require("lodash-es/isObject")):"function"==typeof define&&define.amd?define(["exports","lodash-es/toPath","@rjsf/utils","ajv","ajv-formats","lodash-es/isObject"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self)["@rjsf/validator-ajv8"]={},r.toPath,r.utils,r.Ajv,r.addFormats,r.isObject)}(this,(function(r,e,t,a,s,o){"use strict";function i(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var n=i(e),c=i(a),d=i(s),l=i(o);const u={allErrors:!0,multipleOfPrecision:8},h=/^(#?([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*\)))$/,f=/^data:([a-z]+\/[a-z0-9-+.]+)?;(?:name=(.*);)?base64,(.*)$/,m="__rjsf_rootSchema";class y{constructor(r,e){this.ajv=void 0,this.localizer=void 0;const{additionalMetaSchemas:t,customFormats:a,ajvOptionsOverrides:s,ajvFormatOptions:o}=r;this.ajv=function(r,e,t,a){void 0===t&&(t={});const s=new c.default({...u,...t});return"boolean"!=typeof a&&d.default(s,a),s.addFormat("data-url",f),s.addFormat("color",h),Array.isArray(r)&&s.addMetaSchema(r),l.default(e)&&Object.keys(e).forEach((r=>{s.addFormat(r,e[r])})),s}(t,a,s,o),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 a=[];return t.ERRORS_KEY in r&&(a=a.concat(r.__errors.map((r=>{const t="."+e.join(".");return{property:t,message:r,stack:t+" "+r}})))),Object.keys(r).reduce(((a,s)=>(s!==t.ERRORS_KEY&&(a=a.concat(this.toErrorList(r[s],[...e,s]))),a)),a)}createErrorHandler(r){const e={__errors:[],addError(r){this.__errors.push(r)}};if(t.isObject(r)){const t=r;return Object.keys(t).reduce(((r,e)=>({...r,[e]:this.createErrorHandler(t[e])})),e)}return Array.isArray(r)?r.reduce(((r,e,t)=>({...r,[t]:this.createErrorHandler(e)})),e):e}unwrapErrorHandler(r){return Object.keys(r).reduce(((e,a)=>"addError"===a?e:a===t.ERRORS_KEY?{...e,[a]:r[a]}:{...e,[a]:this.unwrapErrorHandler(r[a])}),{})}transformRJSFValidationErrors(r){return void 0===r&&(r=[]),null===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}}))}validateFormData(r,e,a,s){const o=t.getDefaultFormState(this,e,r,e,!0);let i=null;try{this.ajv.validate(e,o)}catch(r){i=r}"function"==typeof this.localizer&&this.localizer(this.ajv.errors);let n=this.transformRJSFValidationErrors(this.ajv.errors);this.ajv.errors=null;const c=i&&i.message&&"string"==typeof i.message&&i.message.includes("no schema with key or ref ");c&&(n=[...n,{stack:i.message}]),"function"==typeof s&&(n=s(n));let d=this.toErrorSchema(n);if(c&&(d={...d,$schema:{__errors:[i.message]}}),"function"!=typeof a)return{errors:n,errorSchema:d};const l=a(o,this.createErrorHandler(o)),u=this.unwrapErrorHandler(l);return t.mergeValidationData(this,{errors:n,errorSchema:d},u)}withIdRefPrefixObject(r){for(const e in r){const a=r[e];r[e]=e===t.REF_KEY&&"string"==typeof a&&a.startsWith("#")?m+a:this.withIdRefPrefix(a)}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,m).validate(this.withIdRefPrefix(r),e)}catch(r){return!1}finally{this.ajv.removeSchema(m)}}withIdRefPrefix(r){return r.constructor===Object?this.withIdRefPrefixObject({...r}):Array.isArray(r)?this.withIdRefPrefixArray([...r]):r}}function p(r,e){return void 0===r&&(r={}),new y(r,e)}var j=p();r.customizeValidator=p,r.default=j,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("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})})); | ||
//# sourceMappingURL=validator-ajv8.umd.production.min.js.map |
{ | ||
"name": "@rjsf/validator-ajv8", | ||
"version": "5.0.0-beta.11", | ||
"version": "5.0.0-beta.12", | ||
"main": "dist/index.js", | ||
@@ -39,15 +39,15 @@ "module": "dist/validator-ajv8.esm.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.18.13", | ||
"@babel/core": "^7.19.6", | ||
"@babel/plugin-proposal-class-properties": "^7.18.6", | ||
"@babel/plugin-transform-modules-commonjs": "^7.18.6", | ||
"@babel/plugin-transform-react-jsx": "^7.18.10", | ||
"@babel/preset-env": "^7.18.10", | ||
"@babel/plugin-transform-modules-commonjs": "^7.19.6", | ||
"@babel/plugin-transform-react-jsx": "^7.19.0", | ||
"@babel/preset-env": "^7.19.4", | ||
"@babel/preset-react": "^7.18.6", | ||
"@rjsf/utils": "^5.0.0-beta.11", | ||
"@types/jest-expect-message": "^1.0.3", | ||
"@rjsf/utils": "^5.0.0-beta.12", | ||
"@types/jest-expect-message": "^1.1.0", | ||
"@types/json-schema": "^7.0.9", | ||
"@types/lodash": "^4.14.184", | ||
"@types/lodash": "^4.14.186", | ||
"dts-cli": "^1.6.0", | ||
"eslint": "^8.23.0", | ||
"jest-expect-message": "^1.0.2", | ||
"eslint": "^8.26.0", | ||
"jest-expect-message": "^1.1.3", | ||
"rimraf": "^3.0.2" | ||
@@ -76,3 +76,3 @@ }, | ||
"license": "Apache-2.0", | ||
"gitHead": "40b37e064796e4def11f50500ebaf11839191632" | ||
"gitHead": "d0068284cf667418ea236fd4f5c4708f51740961" | ||
} |
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
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
213123
1335