isvalid 3.1.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/validate.js +10 -1
- package/package.json +1 -1
- package/test/index.js +5 -2
- package/test/validate.js +13 -2
package/lib/validate.js
CHANGED
|
@@ -408,7 +408,16 @@ const validateAny = async (data, schema, options, keyPath, validatedData) => {
|
|
|
408
408
|
.filter((validator) => typeof schema[validator] !== 'undefined');
|
|
409
409
|
|
|
410
410
|
for (const pluginValidator of pluginValidators) {
|
|
411
|
-
|
|
411
|
+
try {
|
|
412
|
+
data = await plugins.validate(schema.type, pluginValidator, schema[pluginValidator], data);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
throw new ValidationError(
|
|
415
|
+
keyPath,
|
|
416
|
+
schema._nonFormalizedSchema,
|
|
417
|
+
pluginValidator,
|
|
418
|
+
(schema.errors || {})[pluginValidator] || customErrorMessage((options.errorMessages || {})[pluginValidator] || error.message)
|
|
419
|
+
);
|
|
420
|
+
}
|
|
412
421
|
}
|
|
413
422
|
|
|
414
423
|
return await validatePost(data, schema, options, keyPath, validatedData);
|
package/package.json
CHANGED
package/test/index.js
CHANGED
|
@@ -10,12 +10,15 @@ chai.use(chaiAsPromised);
|
|
|
10
10
|
isvalid.plugins.use(function (utils) {
|
|
11
11
|
return {
|
|
12
12
|
supportsType: (type) => utils.isSameType(type, String),
|
|
13
|
-
validatorsForType: () => { return {
|
|
13
|
+
validatorsForType: () => { return { ensureCase: String }; },
|
|
14
14
|
formalizeValidator: (_, __, config) => {
|
|
15
15
|
if (!config) return;
|
|
16
16
|
if (!caseit.supported.includes(config)) throw new Error(`Only case types: ${caseit.supported.map((casing) => `\`${casing}\``).join(', ')} are supported.`);
|
|
17
17
|
},
|
|
18
|
-
validate: (_, __, config, data) =>
|
|
18
|
+
validate: (_, __, config, data) => {
|
|
19
|
+
if (caseit(data, config) !== data) throw new Error(`Is not ${config} case.`);
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
19
22
|
};
|
|
20
23
|
});
|
|
21
24
|
|
package/test/validate.js
CHANGED
|
@@ -757,8 +757,19 @@ describe('validate', function() {
|
|
|
757
757
|
commonTests.all(Test, new Test(), 123);
|
|
758
758
|
});
|
|
759
759
|
describe('plugin validators', function() {
|
|
760
|
-
it ('should
|
|
761
|
-
return expect(isvalid('my-string', { type: String,
|
|
760
|
+
it ('should throw error if casing does not match.', function() {
|
|
761
|
+
return expect(isvalid('my-string', { type: String, ensureCase: 'camel' }))
|
|
762
|
+
.to.eventually.rejectedWith(ValidationError)
|
|
763
|
+
.and.to.have.property('message', 'Is not camel case.');
|
|
764
|
+
});
|
|
765
|
+
it ('should throw error with custom message if casing does not match.', function() {
|
|
766
|
+
return expect(isvalid('my-string', { type: String, ensureCase: ['camel', 'Something is not right!'] }))
|
|
767
|
+
.to.eventually.rejectedWith(ValidationError)
|
|
768
|
+
.and.to.have.property('message', 'Something is not right!');
|
|
769
|
+
});
|
|
770
|
+
it ('should come back with correct value.', function() {
|
|
771
|
+
return expect(isvalid('myString', { type: String, ensureCase: 'camel' }))
|
|
772
|
+
.to.eventually.equal('myString');
|
|
762
773
|
});
|
|
763
774
|
});
|
|
764
775
|
});
|