@kravc/schema 2.3.2 → 2.3.4
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 +8 -8
- package/src/Validator.js +23 -3
- package/src/Validator.spec.js +31 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kravc/schema",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.4",
|
|
4
4
|
"description": "Advanced JSON schema manipulation and validation library.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"JSON",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"author": "Alexander Kravets <a@kra.vc>",
|
|
27
27
|
"license": "ISC",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"credentials-context": "^
|
|
29
|
+
"credentials-context": "^2.0.0",
|
|
30
30
|
"lodash.clonedeep": "^4.5.0",
|
|
31
31
|
"lodash.groupby": "^4.6.0",
|
|
32
32
|
"lodash.isundefined": "^3.0.1",
|
|
@@ -34,16 +34,16 @@
|
|
|
34
34
|
"lodash.pick": "^4.4.0",
|
|
35
35
|
"lodash.uniq": "^4.5.0",
|
|
36
36
|
"security-context": "^4.0.0",
|
|
37
|
-
"validator": "^13.
|
|
38
|
-
"z-schema": "^5.0.
|
|
37
|
+
"validator": "^13.9.0",
|
|
38
|
+
"z-schema": "^5.0.5"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"chai": "^4.3.
|
|
42
|
-
"eslint": "^8.
|
|
41
|
+
"chai": "^4.3.7",
|
|
42
|
+
"eslint": "^8.35.0",
|
|
43
43
|
"js-yaml": "^4.1.0",
|
|
44
44
|
"jsonld": "^5.2.0",
|
|
45
|
-
"mocha": "^
|
|
46
|
-
"nyc": "^15.0
|
|
45
|
+
"mocha": "^10.2.0",
|
|
46
|
+
"nyc": "^15.1.0"
|
|
47
47
|
},
|
|
48
48
|
"nyc": {
|
|
49
49
|
"include": "src",
|
package/src/Validator.js
CHANGED
|
@@ -46,9 +46,29 @@ class Validator {
|
|
|
46
46
|
throw new Error(`Schema "${schemaId}" not found`)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
const objectJson = JSON.stringify(object)
|
|
50
|
+
const result = JSON.parse(objectJson)
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
// NOTE: Drop attributes from objects that are not defined in schema.
|
|
54
|
+
// This is bad for FE developers, as they continue to send some
|
|
55
|
+
// trash to endpoints, but good for integrations with third party
|
|
56
|
+
// services, e.g. Telegram, when you do not want to define schema
|
|
57
|
+
// for the full payload. This method currently fails for cases when
|
|
58
|
+
// attribute is defined as object or array in schema, but value is
|
|
59
|
+
// a string. In this case validation method below would catch that.
|
|
60
|
+
cleanupAttributes(result, jsonSchema, this._jsonSchemasMap)
|
|
61
|
+
|
|
62
|
+
// NOTE: Normalize method helps to integrate objects built from URLs,
|
|
63
|
+
// where types are not defined, e.g. booleans are '1', 'yes' string
|
|
64
|
+
// or numbers are '1', '2'... strings.
|
|
65
|
+
normalizeAttributes(result, jsonSchema, this._jsonSchemasMap)
|
|
66
|
+
|
|
67
|
+
} catch (error) {
|
|
68
|
+
// NOTE: Skip errors in cleanup and normalize attributes methods,
|
|
69
|
+
// validation fails for objects with invalid value types.
|
|
70
|
+
|
|
71
|
+
}
|
|
52
72
|
|
|
53
73
|
const isValid = this._engine.validate(result, jsonSchema)
|
|
54
74
|
|
package/src/Validator.spec.js
CHANGED
|
@@ -150,6 +150,37 @@ describe('Validator', () => {
|
|
|
150
150
|
}).to.throw('"Profile" validation failed')
|
|
151
151
|
})
|
|
152
152
|
|
|
153
|
+
it('throws validation error if cleanup or normalize method failed', () => {
|
|
154
|
+
const validator = new Validator(SCHEMAS)
|
|
155
|
+
|
|
156
|
+
const input = {
|
|
157
|
+
name: 'Oleksandr',
|
|
158
|
+
contactDetails: {
|
|
159
|
+
email: 'a@kra.vc'
|
|
160
|
+
},
|
|
161
|
+
favoriteItems: 'NOT_ARRAY_BUT_STRING'
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
validator.validate(input, 'Profile')
|
|
166
|
+
|
|
167
|
+
} catch (validationError) {
|
|
168
|
+
const error = validationError.toJSON()
|
|
169
|
+
|
|
170
|
+
expect(error.object).to.exist
|
|
171
|
+
expect(error.code).to.eql('ValidationError')
|
|
172
|
+
expect(error.message).to.eql('"Profile" validation failed')
|
|
173
|
+
expect(error.schemaId).to.eql('Profile')
|
|
174
|
+
|
|
175
|
+
const errorMessage = error.validationErrors[0].message
|
|
176
|
+
expect(errorMessage).to.eql('Expected type array but found type string')
|
|
177
|
+
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
throw new Error('Validation error is not thrown')
|
|
182
|
+
})
|
|
183
|
+
|
|
153
184
|
it('throws error if validation failed', () => {
|
|
154
185
|
const validator = new Validator(SCHEMAS)
|
|
155
186
|
|