@kravc/schema 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/Validator.js
CHANGED
|
@@ -27,7 +27,7 @@ class Validator {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
this._engine = new ZSchema({
|
|
30
|
-
reportPathAsArray:
|
|
30
|
+
reportPathAsArray: false,
|
|
31
31
|
ignoreUnknownFormats: true,
|
|
32
32
|
})
|
|
33
33
|
|
|
@@ -43,7 +43,7 @@ class Validator {
|
|
|
43
43
|
this._jsonSchemasMap = keyBy(jsonSchemas, 'id')
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
validate(object, schemaId,
|
|
46
|
+
validate(object, schemaId, shouldNullifyEmptyValues = false) {
|
|
47
47
|
const jsonSchema = this._jsonSchemasMap[schemaId]
|
|
48
48
|
|
|
49
49
|
if (!jsonSchema) {
|
|
@@ -82,7 +82,7 @@ class Validator {
|
|
|
82
82
|
|
|
83
83
|
let validationErrors = this._engine.getLastErrors()
|
|
84
84
|
|
|
85
|
-
if (!
|
|
85
|
+
if (!shouldNullifyEmptyValues) {
|
|
86
86
|
throw new ValidationError(schemaId, result, validationErrors)
|
|
87
87
|
}
|
|
88
88
|
|
package/src/Validator.spec.js
CHANGED
|
@@ -40,7 +40,7 @@ describe('Validator', () => {
|
|
|
40
40
|
})
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
describe('.validate(object, schemaId,
|
|
43
|
+
describe('.validate(object, schemaId, shouldNullifyEmptyValues = false)', () => {
|
|
44
44
|
it('returns validated, cleaned and normalized object', () => {
|
|
45
45
|
const validator = new Validator(SCHEMAS)
|
|
46
46
|
|
|
@@ -227,7 +227,7 @@ describe('Validator', () => {
|
|
|
227
227
|
})
|
|
228
228
|
})
|
|
229
229
|
|
|
230
|
-
describe('.validate(object, schemaId,
|
|
230
|
+
describe('.validate(object, schemaId, shouldNullifyEmptyValues = false)', () => {
|
|
231
231
|
it('throws validation error for attributes not matching format or pattern', () => {
|
|
232
232
|
const validator = new Validator(SCHEMAS)
|
|
233
233
|
|
|
@@ -247,7 +247,7 @@ describe('Validator', () => {
|
|
|
247
247
|
})
|
|
248
248
|
})
|
|
249
249
|
|
|
250
|
-
describe('.validate(object, schemaId,
|
|
250
|
+
describe('.validate(object, schemaId, shouldNullifyEmptyValues = true)', () => {
|
|
251
251
|
it('returns input with cleaned up null values for not required attributes', () => {
|
|
252
252
|
const validator = new Validator(SCHEMAS)
|
|
253
253
|
|
|
@@ -291,6 +291,7 @@ describe('Validator', () => {
|
|
|
291
291
|
const error = validationError.toJSON()
|
|
292
292
|
|
|
293
293
|
expect(error.message).to.eql('"Profile" validation failed')
|
|
294
|
+
|
|
294
295
|
expect(error.validationErrors).to.have.lengthOf(4)
|
|
295
296
|
|
|
296
297
|
expect(error.validationErrors[0].code).to.eql('INVALID_TYPE')
|
|
@@ -21,7 +21,7 @@ const nullifyEmptyValues = (object, validationErrors) => {
|
|
|
21
21
|
const otherValidationErrors = []
|
|
22
22
|
|
|
23
23
|
for (const error of validationErrors) {
|
|
24
|
-
const { code
|
|
24
|
+
const { code } = error
|
|
25
25
|
|
|
26
26
|
const isAttributeRequired = error[schemaSymbol]['x-required'] === true
|
|
27
27
|
const isFormatError = FORMAT_ERROR_CODES.includes(code)
|
|
@@ -36,6 +36,9 @@ const nullifyEmptyValues = (object, validationErrors) => {
|
|
|
36
36
|
continue
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
const { path: pathString } = error
|
|
40
|
+
const path = pathString.replace('#/', '').split('/')
|
|
41
|
+
|
|
39
42
|
const json = error[jsonSymbol]
|
|
40
43
|
const value = get(json, path)
|
|
41
44
|
|