Skip to content

Commit d4d334f

Browse files
feat: added API to disable and enable validation (#183)
1 parent 02aa068 commit d4d334f

6 files changed

Lines changed: 237 additions & 5 deletions

File tree

declarations/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
import { validate } from "./validate";
22
import { ValidationError } from "./validate";
3-
export { validate, ValidationError };
3+
import { enableValidation } from "./validate";
4+
import { disableValidation } from "./validate";
5+
import { needValidate } from "./validate";
6+
export {
7+
validate,
8+
ValidationError,
9+
enableValidation,
10+
disableValidation,
11+
needValidate,
12+
};

declarations/validate.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ export function validate(
3535
options: Array<object> | object,
3636
configuration?: ValidationErrorConfiguration | undefined
3737
): void;
38+
export function enableValidation(): void;
39+
export function disableValidation(): void;
40+
export function needValidate(): boolean;
3841
import ValidationError from "./ValidationError";
3942
export { ValidationError };

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
const { validate, ValidationError } = require("./validate");
1+
const {
2+
validate,
3+
ValidationError,
4+
enableValidation,
5+
disableValidation,
6+
needValidate,
7+
} = require("./validate");
28

3-
module.exports = { validate, ValidationError };
9+
module.exports = {
10+
validate,
11+
ValidationError,
12+
enableValidation,
13+
disableValidation,
14+
needValidate,
15+
};

src/validate.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,62 @@ function applyPrefix(error, idx) {
105105
return error;
106106
}
107107

108+
let skipValidation = false;
109+
110+
// We use `process.env.SKIP_VALIDATION` because you can have multiple `schema-utils` with different version,
111+
// so we want to disable it globally, `process.env` doesn't supported by browsers, so we have the local `skipValidation` variables
112+
113+
// Enable validation
114+
function enableValidation() {
115+
skipValidation = false;
116+
117+
// Disable validation for any versions
118+
if (process && process.env) {
119+
process.env.SKIP_VALIDATION = "n";
120+
}
121+
}
122+
123+
// Disable validation
124+
function disableValidation() {
125+
skipValidation = true;
126+
127+
if (process && process.env) {
128+
process.env.SKIP_VALIDATION = "y";
129+
}
130+
}
131+
132+
// Check if we need to confirm
133+
function needValidate() {
134+
if (skipValidation) {
135+
return false;
136+
}
137+
138+
if (process && process.env && process.env.SKIP_VALIDATION) {
139+
const value = process.env.SKIP_VALIDATION.trim();
140+
141+
if (/^(?:y|yes|true|1|on)$/i.test(value)) {
142+
return false;
143+
}
144+
145+
if (/^(?:n|no|false|0|off)$/i.test(value)) {
146+
return true;
147+
}
148+
}
149+
150+
return true;
151+
}
152+
108153
/**
109154
* @param {Schema} schema
110155
* @param {Array<object> | object} options
111156
* @param {ValidationErrorConfiguration=} configuration
112157
* @returns {void}
113158
*/
114159
function validate(schema, options, configuration) {
160+
if (!needValidate()) {
161+
return;
162+
}
163+
115164
let errors = [];
116165

117166
if (Array.isArray(options)) {
@@ -191,4 +240,10 @@ function filterErrors(errors) {
191240
return newErrors;
192241
}
193242

194-
export { validate, ValidationError };
243+
export {
244+
validate,
245+
enableValidation,
246+
disableValidation,
247+
needValidate,
248+
ValidationError,
249+
};

test/__snapshots__/api.test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`api should allow to enable validation using "process.env.SKIP_VALIDATION" #2 1`] = `
4+
"Invalid options object. NAME has been initialized using an options object that does not match the API schema.
5+
- options has an unknown property 'foo'. These properties are valid:
6+
object { name? }"
7+
`;
8+
9+
exports[`api should allow to enable validation using "process.env.SKIP_VALIDATION" 1`] = `
10+
"Invalid options object. NAME has been initialized using an options object that does not match the API schema.
11+
- options has an unknown property 'foo'. These properties are valid:
12+
object { name? }"
13+
`;
14+
15+
exports[`api should allow to enable validation using API 1`] = `
16+
"Invalid options object. NAME has been initialized using an options object that does not match the API schema.
17+
- options has an unknown property 'foo'. These properties are valid:
18+
object { name? }"
19+
`;
20+
321
exports[`api should get configuration from schema 1`] = `
422
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
523
- options has an unknown property 'foo'. These properties are valid:

test/api.test.js

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { validate, ValidationError } from "../src/index";
1+
import {
2+
validate,
3+
ValidationError,
4+
enableValidation,
5+
disableValidation,
6+
needValidate,
7+
} from "../src/index";
28

39
import schema from "./fixtures/schema.json";
410
import schemaTitle from "./fixtures/schema-title.json";
@@ -97,4 +103,133 @@ describe("api", () => {
97103
expect(error.message).toMatchSnapshot();
98104
}
99105
});
106+
107+
it('should allow to disable validation using "process.env.SKIP_VALIDATION"', () => {
108+
const oldValue = process.env.SKIP_VALIDATION;
109+
110+
let errored;
111+
112+
process.env.SKIP_VALIDATION = "y";
113+
114+
try {
115+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
116+
} catch (error) {
117+
errored = error;
118+
}
119+
120+
expect(errored).toBeUndefined();
121+
122+
process.env.SKIP_VALIDATION = oldValue;
123+
});
124+
125+
it('should allow to disable validation using "process.env.SKIP_VALIDATION" #2', () => {
126+
const oldValue = process.env.SKIP_VALIDATION;
127+
128+
let errored;
129+
130+
process.env.SKIP_VALIDATION = "YeS";
131+
132+
try {
133+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
134+
} catch (error) {
135+
errored = error;
136+
}
137+
138+
expect(errored).toBeUndefined();
139+
140+
process.env.SKIP_VALIDATION = oldValue;
141+
});
142+
143+
it('should allow to enable validation using "process.env.SKIP_VALIDATION"', () => {
144+
const oldValue = process.env.SKIP_VALIDATION;
145+
146+
process.env.SKIP_VALIDATION = "n";
147+
148+
try {
149+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
150+
} catch (error) {
151+
if (error.name !== "ValidationError") {
152+
throw error;
153+
}
154+
155+
expect(error.message).toMatchSnapshot();
156+
}
157+
158+
process.env.SKIP_VALIDATION = oldValue;
159+
});
160+
161+
it('should allow to enable validation using "process.env.SKIP_VALIDATION" #2', () => {
162+
const oldValue = process.env.SKIP_VALIDATION;
163+
164+
process.env.SKIP_VALIDATION = " FaLse ";
165+
166+
try {
167+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
168+
} catch (error) {
169+
if (error.name !== "ValidationError") {
170+
throw error;
171+
}
172+
173+
expect(error.message).toMatchSnapshot();
174+
}
175+
176+
process.env.SKIP_VALIDATION = oldValue;
177+
});
178+
179+
it("should allow to disable validation using API", () => {
180+
let errored;
181+
182+
disableValidation();
183+
184+
try {
185+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
186+
} catch (error) {
187+
errored = error;
188+
}
189+
190+
expect(errored).toBeUndefined();
191+
192+
enableValidation();
193+
});
194+
195+
it("should allow to enable validation using API", () => {
196+
disableValidation();
197+
enableValidation();
198+
199+
try {
200+
validate(schemaTitle, { foo: "bar" }, { name: "NAME" });
201+
} catch (error) {
202+
if (error.name !== "ValidationError") {
203+
throw error;
204+
}
205+
206+
expect(error.message).toMatchSnapshot();
207+
}
208+
});
209+
210+
it("should allow to enable and disable validation using API", () => {
211+
enableValidation();
212+
expect(needValidate()).toBe(true);
213+
214+
disableValidation();
215+
expect(needValidate()).toBe(false);
216+
enableValidation();
217+
218+
enableValidation();
219+
enableValidation();
220+
expect(needValidate()).toBe(true);
221+
222+
enableValidation();
223+
disableValidation();
224+
expect(needValidate()).toBe(false);
225+
enableValidation();
226+
227+
enableValidation();
228+
expect(process.env.SKIP_VALIDATION).toBe("n");
229+
230+
disableValidation();
231+
expect(process.env.SKIP_VALIDATION).toBe("y");
232+
enableValidation();
233+
expect(process.env.SKIP_VALIDATION).toBe("n");
234+
});
100235
});

0 commit comments

Comments
 (0)