Skip to content

Commit c99019a

Browse files
ref: General refactoring
1 parent 378e087 commit c99019a

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
},
1515
"devDependencies": {
1616
"chai": "4.2.0",
17-
"chai-as-promised": "7.1.1",
1817
"cross-env": "6.0.3",
1918
"mocha": "6.1.4",
2019
"mocha-junit-reporter": "1.22.0",

src/utils/validation-errors-converter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function convertValidationError(error) {
88
const params = errorObject.params
99

1010
switch(errorObject.keyword) {
11+
// TODO Create different error messages for objects and strings #4
1112
case 'type':
1213
return new SchemaValidatorError(`Property '${propName}' must be ${params.type}.`, errorObject.dataPath, errorObject.data)
1314
case 'additionalProperties':

test/test_unit/validation-errors-converter.test.js

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ describe('validation-error-converter test', () => {
66
it('type', () => {
77
const schema = {
88
$id: 'http://example.com/schemas/schema.json',
9-
type: 'object'
9+
type: 'object',
10+
properties: {
11+
typeError: {
12+
type: 'object'
13+
}
14+
}
1015
}
1116
const schemaValidator = new SchemaValidator(schema.$id, [schema])
1217

13-
const result = schemaValidator.validate.bind(schemaValidator, 'invalid-type')
18+
const result = schemaValidator.validate.bind(schemaValidator, {typeError: 'invalid-type'})
1419
expect(result).to.throw().to.include({
15-
field: '',
16-
message: "Property '' must be object.",
20+
field: '.typeError',
21+
message: "Property 'typeError' must be object.",
1722
rejected_value: 'invalid-type'
1823
})
1924
})
@@ -43,54 +48,69 @@ describe('validation-error-converter test', () => {
4348
$id: 'http://example.com/schemas/schema.json',
4449
type: 'object',
4550
additionalProperties: false,
46-
required: ['requiredProperty'],
4751
properties: {
48-
requiredProperty: {
49-
type: 'string'
52+
requiredError: {
53+
type: 'object',
54+
required: ['requiredProperty'],
55+
properties: {
56+
requiredProperty: {
57+
type: 'string'
58+
}
59+
}
5060
}
5161
}
5262
}
5363
const schemaValidator = new SchemaValidator(schema.$id, [schema])
5464

55-
const result = schemaValidator.validate.bind(schemaValidator, {})
65+
const result = schemaValidator.validate.bind(schemaValidator, {requiredError: {}})
5666
expect(result).to.throw().to.include({
57-
field: '.',
67+
field: '.requiredError',
5868
message: "Missing property 'requiredProperty'."
5969
})
6070
})
6171

6272
it('const', () => {
6373
const schema = {
6474
$id: 'http://example.com/schemas/schema.json',
65-
type: 'string',
66-
const: 'valid_value'
75+
type: 'object',
76+
properties: {
77+
constError: {
78+
type: 'string',
79+
const: 'valid_value'
80+
}
81+
}
6782
}
6883
const schemaValidator = new SchemaValidator(schema.$id, [schema])
6984

70-
const result = schemaValidator.validate.bind(schemaValidator, 'invalid_value')
85+
const result = schemaValidator.validate.bind(schemaValidator, {constError: 'invalid_value'})
7186
expect(result).to.throw().to.include({
72-
field: '',
73-
message: "Property '' must have value 'valid_value'.",
87+
field: '.constError',
88+
message: "Property 'constError' must have value 'valid_value'.",
7489
rejected_value: 'invalid_value'
7590
})
7691
})
7792

7893
it('enum - values', () => {
7994
const schema = {
8095
$id: 'http://example.com/schemas/schema.json',
81-
type: 'string',
82-
enum: [
83-
'valid-1',
84-
'valid-2',
85-
'valid-3'
86-
]
96+
type: 'object',
97+
properties: {
98+
enumError: {
99+
type: 'string',
100+
enum: [
101+
'valid-1',
102+
'valid-2',
103+
'valid-3'
104+
]
105+
}
106+
}
87107
}
88108
const schemaValidator = new SchemaValidator(schema.$id, [schema])
89109

90-
const result = schemaValidator.validate.bind(schemaValidator, 'invalid')
110+
const result = schemaValidator.validate.bind(schemaValidator, {enumError: 'invalid'})
91111
expect(result).to.throw().to.include({
92-
field: '',
93-
message: "Property '' with value 'invalid' does not match any allowed value: valid-1, valid-2, valid-3.",
112+
field: '.enumError',
113+
message: "Property 'enumError' with value 'invalid' does not match any allowed value: valid-1, valid-2, valid-3.",
94114
rejected_value: 'invalid'
95115
})
96116
})
@@ -120,31 +140,41 @@ describe('validation-error-converter test', () => {
120140
it('format URL', () => {
121141
const schema = {
122142
$id: 'http://example.com/schemas/schema.json',
123-
type: 'string',
124-
format: 'httpUrl'
143+
type: 'object',
144+
properties: {
145+
formatError: {
146+
type: 'string',
147+
format: 'httpUrl'
148+
}
149+
}
125150
}
126151
const schemaValidator = new SchemaValidator(schema.$id, [schema])
127152

128-
const result = schemaValidator.validate.bind(schemaValidator, 'invalid-url')
153+
const result = schemaValidator.validate.bind(schemaValidator, {formatError: 'invalid-url'})
129154
expect(result).to.throw().to.include({
130-
field: '',
131-
message: `Property '' must be a valid URL. Current value is "invalid-url"`,
155+
field: '.formatError',
156+
message: `Property 'formatError' must be a valid URL. Current value is "invalid-url"`,
132157
rejected_value: 'invalid-url'
133158
})
134159
})
135160

136161
it('format other', () => {
137162
const schema = {
138163
$id: 'http://example.com/schemas/schema.json',
139-
type: 'string',
140-
format: 'email'
164+
type: 'object',
165+
properties: {
166+
formatError: {
167+
type: 'string',
168+
format: 'email'
169+
}
170+
}
141171
}
142172
const schemaValidator = new SchemaValidator(schema.$id, [schema])
143173

144-
const result = schemaValidator.validate.bind(schemaValidator, 'invalid-email')
174+
const result = schemaValidator.validate.bind(schemaValidator, {formatError: 'invalid-email'})
145175
expect(result).to.throw().to.include({
146-
field: '',
147-
message: `Property '' should match format "email"`,
176+
field: '.formatError',
177+
message: `Property 'formatError' should match format "email"`,
148178
rejected_value: 'invalid-email'
149179
})
150180
})

0 commit comments

Comments
 (0)