@kravc/schema 2.5.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/schema",
3
- "version": "2.5.1",
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')
@@ -27,7 +28,7 @@ class Validator {
27
28
  }
28
29
 
29
30
  this._engine = new ZSchema({
30
- reportPathAsArray: true,
31
+ reportPathAsArray: false,
31
32
  ignoreUnknownFormats: true,
32
33
  })
33
34
 
@@ -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
- const result = JSON.parse(objectJson)
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.
@@ -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
- id: '1',
56
- name: 'Student Book',
57
- categories: [ 'Education' ],
58
- _createdAt
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
@@ -291,6 +300,7 @@ describe('Validator', () => {
291
300
  const error = validationError.toJSON()
292
301
 
293
302
  expect(error.message).to.eql('"Profile" validation failed')
303
+
294
304
  expect(error.validationErrors).to.have.lengthOf(4)
295
305
 
296
306
  expect(error.validationErrors[0].code).to.eql('INVALID_TYPE')
@@ -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
+ }
@@ -21,7 +21,7 @@ const nullifyEmptyValues = (object, validationErrors) => {
21
21
  const otherValidationErrors = []
22
22
 
23
23
  for (const error of validationErrors) {
24
- const { code, path } = error
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