@kravc/schema 2.6.0 → 2.7.1
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/eslint.config.mjs +55 -0
- package/package.json +10 -13
- package/src/Schema.js +1 -2
- package/src/ValidationError.js +1 -1
- package/src/Validator.js +9 -4
- package/src/Validator.spec.js +18 -9
- package/src/helpers/cleanupAttributes.js +1 -1
- package/src/helpers/cleanupNulls.js +42 -0
- package/src/helpers/getReferenceIds.js +2 -3
- package/src/helpers/mapObject.js +1 -1
- package/src/helpers/normalizeAttributes.js +1 -1
- package/src/helpers/normalizeProperties.js +1 -1
- package/src/helpers/nullifyEmptyValues.js +1 -2
- package/.eslintrc.yml +0 -42
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import globals from "globals";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import js from "@eslint/js";
|
|
5
|
+
import { FlatCompat } from "@eslint/eslintrc";
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends("eslint:recommended"), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 2018,
|
|
23
|
+
sourceType: "commonjs",
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
"comma-style": "error",
|
|
28
|
+
"consistent-this": ["error", "_this"],
|
|
29
|
+
|
|
30
|
+
indent: ["error", 2, {
|
|
31
|
+
SwitchCase: 1,
|
|
32
|
+
VariableDeclarator: 2,
|
|
33
|
+
}],
|
|
34
|
+
|
|
35
|
+
"keyword-spacing": "error",
|
|
36
|
+
"no-multi-spaces": "off",
|
|
37
|
+
"no-spaced-func": "error",
|
|
38
|
+
"no-trailing-spaces": "error",
|
|
39
|
+
quotes: ["error", "single"],
|
|
40
|
+
semi: ["error", "never"],
|
|
41
|
+
curly: ["error"],
|
|
42
|
+
"prefer-arrow-callback": "error",
|
|
43
|
+
"space-before-blocks": "error",
|
|
44
|
+
|
|
45
|
+
"space-before-function-paren": [1, {
|
|
46
|
+
anonymous: "always",
|
|
47
|
+
named: "never",
|
|
48
|
+
}],
|
|
49
|
+
|
|
50
|
+
"space-infix-ops": "error",
|
|
51
|
+
"space-unary-ops": "error",
|
|
52
|
+
"no-return-await": "error",
|
|
53
|
+
eqeqeq: "error",
|
|
54
|
+
},
|
|
55
|
+
}];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kravc/schema",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Advanced JSON schema manipulation and validation library.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"JSON",
|
|
@@ -27,24 +27,21 @@
|
|
|
27
27
|
"license": "ISC",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"credentials-context": "^2.0.0",
|
|
30
|
-
"lodash
|
|
31
|
-
"lodash.groupby": "^4.6.0",
|
|
32
|
-
"lodash.isundefined": "^3.0.1",
|
|
33
|
-
"lodash.keyby": "^4.6.0",
|
|
34
|
-
"lodash.pick": "^4.4.0",
|
|
35
|
-
"lodash.set": "^4.3.2",
|
|
36
|
-
"lodash.uniq": "^4.5.0",
|
|
30
|
+
"lodash": "^4.17.21",
|
|
37
31
|
"security-context": "^4.0.0",
|
|
38
32
|
"validator": "^13.9.0",
|
|
39
33
|
"z-schema": "^6.0.1"
|
|
40
34
|
},
|
|
41
35
|
"devDependencies": {
|
|
42
|
-
"
|
|
43
|
-
"eslint": "^
|
|
36
|
+
"@eslint/eslintrc": "^3.2.0",
|
|
37
|
+
"@eslint/js": "^9.15.0",
|
|
38
|
+
"chai": "^5.1.2",
|
|
39
|
+
"eslint": "^9.15.0",
|
|
40
|
+
"globals": "^15.12.0",
|
|
44
41
|
"js-yaml": "^4.1.0",
|
|
45
|
-
"jsonld": "^
|
|
46
|
-
"mocha": "^10.2
|
|
47
|
-
"nyc": "^
|
|
42
|
+
"jsonld": "^8.3.2",
|
|
43
|
+
"mocha": "^10.8.2",
|
|
44
|
+
"nyc": "^17.1.0"
|
|
48
45
|
},
|
|
49
46
|
"nyc": {
|
|
50
47
|
"include": "src",
|
package/src/Schema.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const pick
|
|
4
|
-
const cloneDeep = require('lodash.clonedeep')
|
|
3
|
+
const { pick, cloneDeep } = require('lodash')
|
|
5
4
|
const validateId = require('./helpers/validateId')
|
|
6
5
|
const normalizeRequired = require('./helpers/normalizeRequired')
|
|
7
6
|
const normalizeProperties = require('./helpers/normalizeProperties')
|
package/src/ValidationError.js
CHANGED
package/src/Validator.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const keyBy
|
|
4
|
-
const groupBy = require('lodash.groupby')
|
|
3
|
+
const { keyBy, groupBy } = require('lodash')
|
|
5
4
|
const ZSchema = require('z-schema')
|
|
5
|
+
const cleanupNulls = require('./helpers/cleanupNulls')
|
|
6
6
|
const getReferenceIds = require('./helpers/getReferenceIds')
|
|
7
7
|
const ValidationError = require('./ValidationError')
|
|
8
8
|
const cleanupAttributes = require('./helpers/cleanupAttributes')
|
|
@@ -43,7 +43,7 @@ class Validator {
|
|
|
43
43
|
this._jsonSchemasMap = keyBy(jsonSchemas, 'id')
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
validate(object, schemaId, shouldNullifyEmptyValues = false) {
|
|
46
|
+
validate(object, schemaId, shouldNullifyEmptyValues = false, shouldCleanupNulls = false) {
|
|
47
47
|
const jsonSchema = this._jsonSchemasMap[schemaId]
|
|
48
48
|
|
|
49
49
|
if (!jsonSchema) {
|
|
@@ -51,7 +51,11 @@ class Validator {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
const objectJson = JSON.stringify(object)
|
|
54
|
-
|
|
54
|
+
let result = JSON.parse(objectJson)
|
|
55
|
+
|
|
56
|
+
if (shouldCleanupNulls) {
|
|
57
|
+
result = cleanupNulls(result)
|
|
58
|
+
}
|
|
55
59
|
|
|
56
60
|
try {
|
|
57
61
|
// NOTE: Drop attributes from objects that are not defined in schema.
|
|
@@ -68,6 +72,7 @@ class Validator {
|
|
|
68
72
|
// or numbers are '1', '2'... strings.
|
|
69
73
|
normalizeAttributes(result, jsonSchema, this._jsonSchemasMap)
|
|
70
74
|
|
|
75
|
+
// eslint-disable-next-line no-unused-vars
|
|
71
76
|
} catch (error) {
|
|
72
77
|
// NOTE: Skip errors in cleanup and normalize attributes methods,
|
|
73
78
|
// validation fails for objects with invalid value types.
|
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,42 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { isArray } = Array
|
|
4
|
+
const { isObject, cloneDeep } = require('lodash')
|
|
5
|
+
|
|
6
|
+
const cleanupNulls = object => {
|
|
7
|
+
if (!isObject(object)) {
|
|
8
|
+
return
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
for (const key in object) {
|
|
12
|
+
const value = object[key]
|
|
13
|
+
|
|
14
|
+
if (isArray(value)) {
|
|
15
|
+
for (const item of value) {
|
|
16
|
+
cleanupNulls(item)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (isObject(value)) {
|
|
23
|
+
cleanupNulls(value)
|
|
24
|
+
|
|
25
|
+
continue
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const isNull = value === null
|
|
29
|
+
|
|
30
|
+
if (isNull) {
|
|
31
|
+
delete object[key]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = input => {
|
|
37
|
+
const object = cloneDeep(input)
|
|
38
|
+
|
|
39
|
+
cleanupNulls(object)
|
|
40
|
+
|
|
41
|
+
return object
|
|
42
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const uniq
|
|
4
|
-
const Schema
|
|
5
|
-
const isUndefined = require('lodash.isundefined')
|
|
3
|
+
const { isUndefined, uniq } = require('lodash')
|
|
4
|
+
const Schema = require('../Schema')
|
|
6
5
|
|
|
7
6
|
const getReferenceIds = (schema, schemasMap) => {
|
|
8
7
|
const getSchema = id => schemasMap[id]
|
package/src/helpers/mapObject.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const mapObject = require('./mapObject')
|
|
4
|
-
const isUndefined = require('lodash.isundefined')
|
|
5
4
|
const normalizeType = require('./normalizeType')
|
|
5
|
+
const { isUndefined } = require('lodash')
|
|
6
6
|
|
|
7
7
|
const normalizeAttributes = (object, jsonSchema, jsonSchemasMap) => {
|
|
8
8
|
const callback = (propertyName, propertySchema, object) => {
|
package/.eslintrc.yml
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
extends: 'eslint:recommended'
|
|
2
|
-
|
|
3
|
-
parserOptions:
|
|
4
|
-
ecmaVersion: 2018
|
|
5
|
-
|
|
6
|
-
env:
|
|
7
|
-
es6: true
|
|
8
|
-
node: true
|
|
9
|
-
mocha: true
|
|
10
|
-
|
|
11
|
-
rules:
|
|
12
|
-
comma-style: error
|
|
13
|
-
consistent-this:
|
|
14
|
-
- error
|
|
15
|
-
- _this
|
|
16
|
-
indent:
|
|
17
|
-
- error
|
|
18
|
-
- 2
|
|
19
|
-
- SwitchCase: 1
|
|
20
|
-
VariableDeclarator: 2
|
|
21
|
-
keyword-spacing: error
|
|
22
|
-
no-multi-spaces: off
|
|
23
|
-
no-spaced-func: error
|
|
24
|
-
no-trailing-spaces: error
|
|
25
|
-
quotes:
|
|
26
|
-
- error
|
|
27
|
-
- single
|
|
28
|
-
semi:
|
|
29
|
-
- error
|
|
30
|
-
- never
|
|
31
|
-
curly:
|
|
32
|
-
- error
|
|
33
|
-
prefer-arrow-callback: error
|
|
34
|
-
space-before-blocks: error
|
|
35
|
-
space-before-function-paren:
|
|
36
|
-
- 1
|
|
37
|
-
- anonymous: always
|
|
38
|
-
named: never
|
|
39
|
-
space-infix-ops: error
|
|
40
|
-
space-unary-ops: error
|
|
41
|
-
no-return-await: error
|
|
42
|
-
eqeqeq: error
|