Skip to content

Commit ff0237f

Browse files
committed
chore: cleanup
1 parent e84f1d6 commit ff0237f

File tree

2 files changed

+101
-95
lines changed

2 files changed

+101
-95
lines changed
Lines changed: 4 additions & 95 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, getDeprecationWarnings } = require('./utils');
5+
116
module.exports = {
12-
createValidator,
13-
withErrorMessages,
14-
withDeprecationWarnings
7+
createValidator
158
};
169

1710
function createValidator(schema, errors, deprecations) {
@@ -57,13 +50,6 @@ function createValidator(schema, errors, deprecations) {
5750

5851
const validator = ajv.compile(withErrorMessages(withDeprecationWarnings(schema, deprecations), errors));
5952

60-
61-
const getDeprecationWarnings = function(data) {
62-
if (deprecationWarnings.length > 0 && data.id === deprecationWarnings[0].id) {
63-
return deprecationWarnings.map(warning => warning.warningDescription);
64-
}
65-
};
66-
6753
// wrapper function checks for warnings before each validation
6854
const wrappedValidator = function(data, ...args) {
6955

@@ -73,87 +59,10 @@ function createValidator(schema, errors, deprecations) {
7359
const result = validator.call(this, data, ...args);
7460

7561
wrappedValidator.errors = validator.errors;
76-
wrappedValidator.warnings = getDeprecationWarnings(data);
62+
wrappedValidator.warnings = getDeprecationWarnings(deprecationWarnings, data);
7763

7864
return result;
7965
};
8066

8167
return wrappedValidator;
82-
}
83-
84-
function withErrorMessages(schema, errors) {
85-
86-
if (!errors || !errors.length) {
87-
return schema;
88-
}
89-
90-
// clone a new copy
91-
let newSchema = JSON.parse(JSON.stringify(schema));
92-
93-
// set <errorMessage> keyword for given path
94-
forEach(errors, function(error) {
95-
newSchema = setErrorMessage(newSchema, error);
96-
});
97-
98-
return newSchema;
99-
}
100-
101-
function setErrorMessage(schema, error) {
102-
const {
103-
path,
104-
errorMessage
105-
} = error;
106-
107-
const errorMessagePath = [
108-
...path,
109-
'errorMessage'
110-
];
111-
112-
return set(schema, errorMessagePath, errorMessage);
113-
}
114-
115-
function eqlErrors(chai, utils) {
116-
117-
const Assertion = chai.Assertion;
118-
119-
Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
120-
121-
const actualErrors = this._obj;
122-
123-
// formats the validation errors, so that they can be used directly in the fixture files.
124-
this.eql(expectedErrors,
125-
`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`);
126-
});
127-
}
128-
129-
chai.use(eqlErrors);
130-
131-
function withDeprecationWarnings(schema, deprecations) {
132-
if (!deprecations || !deprecations.length) {
133-
return schema;
134-
}
135-
136-
// clone a new copy
137-
let newSchema = JSON.parse(JSON.stringify(schema));
138-
139-
// set deprecation warnings for given paths
140-
forEach(deprecations, function(deprecation) {
141-
newSchema = setDeprecationWarning(newSchema, deprecation);
142-
});
143-
144-
return newSchema;
145-
}
146-
147-
function setDeprecationWarning(schema, deprecation) {
148-
const {
149-
path,
150-
warningMessage
151-
} = deprecation;
152-
153-
const deprecationPath = [
154-
...path,
155-
'deprecatedWarning'
156-
];
157-
158-
return set(schema, deprecationPath, warningMessage);
15968
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 getDeprecationWarnings(deprecationWarnings, data) {
71+
if (deprecationWarnings.length > 0 && data.id === deprecationWarnings[0].id) {
72+
return deprecationWarnings.map(warning => warning.warningDescription);
73+
}
74+
};
75+
76+
function eqlErrors(chai, utils) {
77+
78+
const Assertion = chai.Assertion;
79+
80+
Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
81+
82+
const actualErrors = this._obj;
83+
84+
// formats the validation errors, so that they can be used directly in the fixture files.
85+
this.eql(expectedErrors,
86+
`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`);
87+
});
88+
}
89+
90+
chai.use(eqlErrors);
91+
92+
module.exports = {
93+
withErrorMessages,
94+
withDeprecationWarnings,
95+
getDeprecationWarnings
96+
};
97+

0 commit comments

Comments
 (0)