Skip to content

Commit dc6ecb5

Browse files
committed
chore: cleanup
1 parent 0efcede commit dc6ecb5

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) {
@@ -59,13 +52,6 @@ function createValidator(schema, errors, deprecations) {
5952

6053
const validator = ajv.compile(withErrorMessages(withDeprecationWarnings(schema, deprecations), errors));
6154

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

@@ -75,87 +61,10 @@ function createValidator(schema, errors, deprecations) {
7561
const result = validator.call(this, data, ...args);
7662

7763
wrappedValidator.errors = validator.errors;
78-
wrappedValidator.warnings = getDeprecationWarnings(data);
64+
wrappedValidator.warnings = getDeprecationWarnings(deprecationWarnings, data);
7965

8066
return result;
8167
};
8268

8369
return wrappedValidator;
84-
}
85-
86-
function withErrorMessages(schema, errors) {
87-
88-
if (!errors || !errors.length) {
89-
return schema;
90-
}
91-
92-
// clone a new copy
93-
let newSchema = JSON.parse(JSON.stringify(schema));
94-
95-
// set <errorMessage> keyword for given path
96-
forEach(errors, function(error) {
97-
newSchema = setErrorMessage(newSchema, error);
98-
});
99-
100-
return newSchema;
101-
}
102-
103-
function setErrorMessage(schema, error) {
104-
const {
105-
path,
106-
errorMessage
107-
} = error;
108-
109-
const errorMessagePath = [
110-
...path,
111-
'errorMessage'
112-
];
113-
114-
return set(schema, errorMessagePath, errorMessage);
115-
}
116-
117-
function eqlErrors(chai, utils) {
118-
119-
const Assertion = chai.Assertion;
120-
121-
Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
122-
123-
const actualErrors = this._obj;
124-
125-
// formats the validation errors, so that they can be used directly in the fixture files.
126-
this.eql(expectedErrors,
127-
`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`);
128-
});
129-
}
130-
131-
chai.use(eqlErrors);
132-
133-
function withDeprecationWarnings(schema, deprecations) {
134-
if (!deprecations || !deprecations.length) {
135-
return schema;
136-
}
137-
138-
// clone a new copy
139-
let newSchema = JSON.parse(JSON.stringify(schema));
140-
141-
// set deprecation warnings for given paths
142-
forEach(deprecations, function(deprecation) {
143-
newSchema = setDeprecationWarning(newSchema, deprecation);
144-
});
145-
146-
return newSchema;
147-
}
148-
149-
function setDeprecationWarning(schema, deprecation) {
150-
const {
151-
path,
152-
warningMessage
153-
} = deprecation;
154-
155-
const deprecationPath = [
156-
...path,
157-
'deprecatedWarning'
158-
];
159-
160-
return set(schema, deprecationPath, warningMessage);
16170
}
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)