validate-value
Advanced tools
Comparing version 7.1.1 to 7.2.0
@@ -1,6 +0,6 @@ | ||
import { ValidationError as OriginError } from '@exodus/schemasafe'; | ||
import Ajv from 'ajv'; | ||
declare class ValidationError extends Error { | ||
origin: OriginError; | ||
constructor(message: string, origin: OriginError); | ||
origin: Ajv.ErrorObject; | ||
constructor(message: string, origin: Ajv.ErrorObject); | ||
} | ||
export { ValidationError }; |
@@ -0,6 +1,6 @@ | ||
import Ajv from 'ajv'; | ||
import { JSONSchema4 } from 'json-schema'; | ||
import { Validate } from '@exodus/schemasafe'; | ||
declare class Value { | ||
schema: JSONSchema4; | ||
protected validateInternal: Validate; | ||
protected validateInternal: Ajv.ValidateFunction; | ||
constructor(schema: JSONSchema4); | ||
@@ -7,0 +7,0 @@ validate(value: any, { valueName, separator }?: { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Value = void 0; | ||
const ajv_1 = __importDefault(require("ajv")); | ||
const ValidationError_1 = require("./ValidationError"); | ||
const schemasafe_1 = require("@exodus/schemasafe"); | ||
const ajvInstance = new ajv_1.default(); | ||
ajvInstance.addFormat('alphanumeric', /[a-zA-Z0-9]/u); | ||
class Value { | ||
constructor(schema) { | ||
this.schema = schema; | ||
this.validateInternal = schemasafe_1.validator(schema, { | ||
extraFormats: true, | ||
includeErrors: true | ||
}); | ||
this.validateInternal = ajvInstance.compile(schema); | ||
} | ||
@@ -20,6 +22,10 @@ validate(value, { valueName = 'value', separator = '.' } = {}) { | ||
const error = this.validateInternal.errors[0]; | ||
const updatedPath = `${valueName}${error.instanceLocation.slice(1).replace(/\//gu, separator)}`; | ||
let updatedPath = `${valueName}${error.dataPath.slice(1).replace(/\//gu, separator)}`; | ||
let message = 'Validation failed'; | ||
if (error.keywordLocation.endsWith('/required')) { | ||
message = `Missing required property: ${updatedPath.slice(updatedPath.lastIndexOf(separator) + 1)}`; | ||
if (error.keyword === 'required') { | ||
const missingPropertyName = error.params.missingProperty; | ||
message = `Missing required property: ${missingPropertyName}`; | ||
// Ajv treats missing required properties as errors on the object that should have the property, so the name of | ||
// the missing property is missing in the data path and must be appended. | ||
updatedPath += `${separator}${missingPropertyName}`; | ||
} | ||
@@ -26,0 +32,0 @@ throw new ValidationError_1.ValidationError(`${message} (at ${updatedPath}).`, error); |
@@ -0,1 +1,8 @@ | ||
# [7.2.0](https://github.com/thenativeweb/validate-value/compare/7.1.1...7.2.0) (2020-09-29) | ||
### Features | ||
* Replace @exodus/schemasafe with ajv. ([#177](https://github.com/thenativeweb/validate-value/issues/177)) ([2679a4e](https://github.com/thenativeweb/validate-value/commit/2679a4ee18b74e1ae050df7a9b895f6e402e7830)) | ||
## [7.1.1](https://github.com/thenativeweb/validate-value/compare/7.1.0...7.1.1) (2020-09-13) | ||
@@ -2,0 +9,0 @@ |
@@ -1,7 +0,7 @@ | ||
import { ValidationError as OriginError } from '@exodus/schemasafe'; | ||
import Ajv from 'ajv'; | ||
class ValidationError extends Error { | ||
public origin: OriginError; | ||
public origin: Ajv.ErrorObject; | ||
public constructor (message: string, origin: OriginError) { | ||
public constructor (message: string, origin: Ajv.ErrorObject) { | ||
super(message); | ||
@@ -8,0 +8,0 @@ this.origin = origin; |
@@ -0,16 +1,17 @@ | ||
import Ajv from 'ajv'; | ||
import { JSONSchema4 } from 'json-schema'; | ||
import { ValidationError } from './ValidationError'; | ||
import { Validate, validator } from '@exodus/schemasafe'; | ||
const ajvInstance: Ajv.Ajv = new Ajv(); | ||
ajvInstance.addFormat('alphanumeric', /[a-zA-Z0-9]/u); | ||
class Value { | ||
public schema: JSONSchema4; | ||
protected validateInternal: Validate; | ||
protected validateInternal: Ajv.ValidateFunction; | ||
public constructor (schema: JSONSchema4) { | ||
this.schema = schema; | ||
this.validateInternal = validator(schema, { | ||
extraFormats: true, | ||
includeErrors: true | ||
}); | ||
this.validateInternal = ajvInstance.compile(schema); | ||
} | ||
@@ -30,7 +31,13 @@ | ||
const updatedPath = `${valueName}${error.instanceLocation.slice(1).replace(/\//gu, separator)}`; | ||
let updatedPath = `${valueName}${error.dataPath.slice(1).replace(/\//gu, separator)}`; | ||
let message = 'Validation failed'; | ||
if (error.keywordLocation.endsWith('/required')) { | ||
message = `Missing required property: ${updatedPath.slice(updatedPath.lastIndexOf(separator) + 1)}`; | ||
if (error.keyword === 'required') { | ||
const missingPropertyName = (error.params as Ajv.RequiredParams).missingProperty; | ||
message = `Missing required property: ${missingPropertyName}`; | ||
// Ajv treats missing required properties as errors on the object that should have the property, so the name of | ||
// the missing property is missing in the data path and must be appended. | ||
updatedPath += `${separator}${missingPropertyName}`; | ||
} | ||
@@ -37,0 +44,0 @@ |
{ | ||
"name": "validate-value", | ||
"version": "7.1.1", | ||
"version": "7.2.0", | ||
"description": "validate-value validates values against JSON schemas.", | ||
@@ -22,3 +22,3 @@ "contributors": [ | ||
"dependencies": { | ||
"@exodus/schemasafe": "1.0.0-rc.2", | ||
"ajv": "6.12.5", | ||
"json-schema": "0.2.5" | ||
@@ -29,3 +29,3 @@ }, | ||
"roboter": "11.2.28", | ||
"semantic-release-configuration": "1.0.20" | ||
"semantic-release-configuration": "1.0.21" | ||
}, | ||
@@ -32,0 +32,0 @@ "repository": { |
@@ -9,5 +9,2 @@ { | ||
"outDir": "build", | ||
"paths": { | ||
"@exodus/schemasafe": [ "./types/@exodus/schemasafe" ] | ||
}, | ||
"resolveJsonModule": true, | ||
@@ -14,0 +11,0 @@ "strict": true, |
10448
13
145
+ Addedajv@6.12.5
+ Addedajv@6.12.5(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addeduri-js@4.4.1(transitive)
- Removed@exodus/schemasafe@1.0.0-rc.2
- Removed@exodus/schemasafe@1.0.0-rc.2(transitive)