Skip to content

Commit 8c8e168

Browse files
committed
chore: cleanup
1 parent 8577273 commit 8c8e168

File tree

2 files changed

+93
-86
lines changed

2 files changed

+93
-86
lines changed
Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
const {
2-
forEach,
3-
set
4-
} = require('min-dash');
5-
6-
const chai = require('chai');
7-
81
const { default: Ajv } = require('ajv');
92
const AjvErrors = require('ajv-errors');
103

4+
const { withErrorMessages, withDeprecationWarnings } = require('./utils');
5+
116
module.exports = {
12-
createValidator,
13-
withErrorMessages,
14-
withDeprecationWarnings
7+
createValidator
158
};
169

1710
function createValidator(schema, errors, deprecations) {
@@ -80,79 +73,3 @@ function createValidator(schema, errors, deprecations) {
8073
return wrappedValidator;
8174
}
8275

83-
function withErrorMessages(schema, errors) {
84-
85-
if (!errors || !errors.length) {
86-
return schema;
87-
}
88-
89-
// clone a new copy
90-
let newSchema = JSON.parse(JSON.stringify(schema));
91-
92-
// set <errorMessage> keyword for given path
93-
forEach(errors, function(error) {
94-
newSchema = setErrorMessage(newSchema, error);
95-
});
96-
97-
return newSchema;
98-
}
99-
100-
function setErrorMessage(schema, error) {
101-
const {
102-
path,
103-
errorMessage
104-
} = error;
105-
106-
const errorMessagePath = [
107-
...path,
108-
'errorMessage'
109-
];
110-
111-
return set(schema, errorMessagePath, errorMessage);
112-
}
113-
114-
function eqlErrors(chai, utils) {
115-
116-
const Assertion = chai.Assertion;
117-
118-
Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
119-
120-
const actualErrors = this._obj;
121-
122-
// formats the validation errors, so that they can be used directly in the fixture files.
123-
this.eql(expectedErrors,
124-
`Errors from validation do not match expected.\n\tValidation returned this error (you can use it in the fixture):\n\t${JSON.stringify(actualErrors, null, 2).replace(/"([^"]+)":/g, '$1:')}\n`);
125-
});
126-
}
127-
128-
chai.use(eqlErrors);
129-
130-
function withDeprecationWarnings(schema, deprecations) {
131-
if (!deprecations || !deprecations.length) {
132-
return schema;
133-
}
134-
135-
// clone a new copy
136-
let newSchema = JSON.parse(JSON.stringify(schema));
137-
138-
// set deprecation warnings for given paths
139-
forEach(deprecations, function(deprecation) {
140-
newSchema = setDeprecationWarning(newSchema, deprecation);
141-
});
142-
143-
return newSchema;
144-
}
145-
146-
function setDeprecationWarning(schema, deprecation) {
147-
const {
148-
path,
149-
warningMessage
150-
} = deprecation;
151-
152-
const deprecationPath = [
153-
...path,
154-
'deprecatedWarning'
155-
];
156-
157-
return set(schema, deprecationPath, warningMessage);
158-
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const chai = require('chai');
2+
3+
const {
4+
forEach,
5+
set
6+
} = require('min-dash');
7+
8+
function withErrorMessages(schema, errors) {
9+
10+
if (!errors || !errors.length) {
11+
return schema;
12+
}
13+
14+
// clone a new copy
15+
let newSchema = JSON.parse(JSON.stringify(schema));
16+
17+
// set <errorMessage> keyword for given path
18+
forEach(errors, function(error) {
19+
newSchema = setErrorMessage(newSchema, error);
20+
});
21+
22+
return newSchema;
23+
}
24+
25+
function setErrorMessage(schema, error) {
26+
const {
27+
path,
28+
errorMessage
29+
} = error;
30+
31+
const errorMessagePath = [
32+
...path,
33+
'errorMessage'
34+
];
35+
36+
return set(schema, errorMessagePath, errorMessage);
37+
}
38+
39+
40+
function withDeprecationWarnings(schema, deprecations) {
41+
if (!deprecations || !deprecations.length) {
42+
return schema;
43+
}
44+
45+
// clone a new copy
46+
let newSchema = JSON.parse(JSON.stringify(schema));
47+
48+
// set deprecation warnings for given paths
49+
forEach(deprecations, function(deprecation) {
50+
newSchema = setDeprecationWarning(newSchema, deprecation);
51+
});
52+
53+
return newSchema;
54+
}
55+
56+
function setDeprecationWarning(schema, deprecation) {
57+
const {
58+
path,
59+
warningMessage
60+
} = deprecation;
61+
62+
const deprecationPath = [
63+
...path,
64+
'deprecatedWarning'
65+
];
66+
67+
return set(schema, deprecationPath, warningMessage);
68+
}
69+
70+
function eqlErrors(chai, utils) {
71+
72+
const Assertion = chai.Assertion;
73+
74+
Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
75+
76+
const actualErrors = this._obj;
77+
78+
// formats the validation errors, so that they can be used directly in the fixture files.
79+
this.eql(expectedErrors,
80+
`Errors from validation do not match expected.\n\tValidation returned this error (you can use it in the fixture):\n\t${JSON.stringify(actualErrors, null, 2).replace(/"([^"]+)":/g, '$1:')}\n`);
81+
});
82+
}
83+
84+
chai.use(eqlErrors);
85+
86+
module.exports = {
87+
withErrorMessages,
88+
withDeprecationWarnings
89+
};
90+

0 commit comments

Comments
 (0)