@kravc/schema 2.6.0 → 2.7.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 +2 -1
- package/src/Validator.js +7 -2
- package/src/Validator.spec.js +18 -9
- package/src/helpers/cleanupNulls.js +43 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kravc/schema",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Advanced JSON schema manipulation and validation library.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"JSON",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"credentials-context": "^2.0.0",
|
|
30
30
|
"lodash.clonedeep": "^4.5.0",
|
|
31
31
|
"lodash.groupby": "^4.6.0",
|
|
32
|
+
"lodash.isobject": "^3.0.2",
|
|
32
33
|
"lodash.isundefined": "^3.0.1",
|
|
33
34
|
"lodash.keyby": "^4.6.0",
|
|
34
35
|
"lodash.pick": "^4.4.0",
|
package/src/Validator.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const keyBy = require('lodash.keyby')
|
|
4
4
|
const groupBy = require('lodash.groupby')
|
|
5
5
|
const ZSchema = require('z-schema')
|
|
6
|
+
const cleanupNulls = require('./helpers/cleanupNulls')
|
|
6
7
|
const getReferenceIds = require('./helpers/getReferenceIds')
|
|
7
8
|
const ValidationError = require('./ValidationError')
|
|
8
9
|
const cleanupAttributes = require('./helpers/cleanupAttributes')
|
|
@@ -43,7 +44,7 @@ class Validator {
|
|
|
43
44
|
this._jsonSchemasMap = keyBy(jsonSchemas, 'id')
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
validate(object, schemaId, shouldNullifyEmptyValues = false) {
|
|
47
|
+
validate(object, schemaId, shouldNullifyEmptyValues = false, shouldCleanupNulls = false) {
|
|
47
48
|
const jsonSchema = this._jsonSchemasMap[schemaId]
|
|
48
49
|
|
|
49
50
|
if (!jsonSchema) {
|
|
@@ -51,7 +52,11 @@ class Validator {
|
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
const objectJson = JSON.stringify(object)
|
|
54
|
-
|
|
55
|
+
let result = JSON.parse(objectJson)
|
|
56
|
+
|
|
57
|
+
if (shouldCleanupNulls) {
|
|
58
|
+
result = cleanupNulls(result)
|
|
59
|
+
}
|
|
55
60
|
|
|
56
61
|
try {
|
|
57
62
|
// NOTE: Drop attributes from objects that are not defined in schema.
|
package/src/Validator.spec.js
CHANGED
|
@@ -40,7 +40,7 @@ describe('Validator', () => {
|
|
|
40
40
|
})
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
describe('.validate(object, schemaId, shouldNullifyEmptyValues = false)', () => {
|
|
43
|
+
describe('.validate(object, schemaId, shouldNullifyEmptyValues = false, shouldCleanupNulls = true)', () => {
|
|
44
44
|
it('returns validated, cleaned and normalized object', () => {
|
|
45
45
|
const validator = new Validator(SCHEMAS)
|
|
46
46
|
|
|
@@ -48,15 +48,20 @@ describe('Validator', () => {
|
|
|
48
48
|
|
|
49
49
|
const input = {
|
|
50
50
|
name: 'Oleksandr',
|
|
51
|
+
toBeRemoved: null,
|
|
51
52
|
contactDetails: {
|
|
52
|
-
email: 'a@kra.vc'
|
|
53
|
+
email: 'a@kra.vc',
|
|
54
|
+
toBeRemoved: null,
|
|
53
55
|
},
|
|
54
|
-
favoriteItems: [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
favoriteItems: [
|
|
57
|
+
{
|
|
58
|
+
id: '1',
|
|
59
|
+
name: 'Student Book',
|
|
60
|
+
categories: [ 'Education' ],
|
|
61
|
+
toBeRemoved: null,
|
|
62
|
+
_createdAt
|
|
63
|
+
},
|
|
64
|
+
],
|
|
60
65
|
locations: [{
|
|
61
66
|
name: 'Home',
|
|
62
67
|
address: {
|
|
@@ -77,7 +82,11 @@ describe('Validator', () => {
|
|
|
77
82
|
_createdAt
|
|
78
83
|
}
|
|
79
84
|
|
|
80
|
-
const validInput = validator.validate(input, 'Profile')
|
|
85
|
+
const validInput = validator.validate(input, 'Profile', false, true)
|
|
86
|
+
|
|
87
|
+
expect(validInput.toBeRemoved).to.not.exist
|
|
88
|
+
expect(validInput.contactDetails.toBeRemoved).to.not.exist
|
|
89
|
+
expect(validInput.favoriteItems[0].toBeRemoved).to.not.exist
|
|
81
90
|
|
|
82
91
|
expect(validInput._createdAt).to.not.exist
|
|
83
92
|
expect(validInput.preferences._createdAt).to.not.exist
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const isObject = require('lodash.isobject')
|
|
4
|
+
const cloneDeep = require('lodash.clonedeep')
|
|
5
|
+
const { isArray } = Array
|
|
6
|
+
|
|
7
|
+
const cleanupNulls = object => {
|
|
8
|
+
if (!isObject(object)) {
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (const key in object) {
|
|
13
|
+
const value = object[key]
|
|
14
|
+
|
|
15
|
+
if (isArray(value)) {
|
|
16
|
+
for (const item of value) {
|
|
17
|
+
cleanupNulls(item)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
continue
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (isObject(value)) {
|
|
24
|
+
cleanupNulls(value)
|
|
25
|
+
|
|
26
|
+
continue
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const isNull = value === null
|
|
30
|
+
|
|
31
|
+
if (isNull) {
|
|
32
|
+
delete object[key]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = input => {
|
|
38
|
+
const object = cloneDeep(input)
|
|
39
|
+
|
|
40
|
+
cleanupNulls(object)
|
|
41
|
+
|
|
42
|
+
return object
|
|
43
|
+
}
|