isvalid 4.0.8 → 4.0.9
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/formalize.js +8 -2
- package/lib/validate.js +1 -1
- package/package.json +1 -1
- package/test/validate.js +8 -8
package/lib/formalize.js
CHANGED
|
@@ -96,10 +96,14 @@ const formalizeAny = (schema, options = {}) => {
|
|
|
96
96
|
throw new SchemaError(schema, 'Schemas must have at least on validator of `type`, `post`/`pre` and/or `equal`.');
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
const convenienceNames = {
|
|
100
|
+
'autowrap': 'autoWrap' // Fix younger Kristian
|
|
101
|
+
};
|
|
102
|
+
|
|
99
103
|
// Copy schema.
|
|
100
104
|
let formalizedSchema = {};
|
|
101
105
|
for (let key in schema) {
|
|
102
|
-
formalizedSchema[key] = schema[key];
|
|
106
|
+
formalizedSchema[convenienceNames[key] || key] = schema[key];
|
|
103
107
|
}
|
|
104
108
|
|
|
105
109
|
// Validators common to all types.
|
|
@@ -126,7 +130,7 @@ const formalizeAny = (schema, options = {}) => {
|
|
|
126
130
|
'schema': 'any',
|
|
127
131
|
'len': [ 'string', 'number' ],
|
|
128
132
|
'unique': [ 'boolean' ],
|
|
129
|
-
'
|
|
133
|
+
'autoWrap': [ 'boolean' ]
|
|
130
134
|
});
|
|
131
135
|
if (isSameType('string', typeName(type))) merge(validators, {
|
|
132
136
|
'len': [ 'string', 'number' ],
|
|
@@ -149,6 +153,8 @@ const formalizeAny = (schema, options = {}) => {
|
|
|
149
153
|
// for non-supported validators at the same time.
|
|
150
154
|
for (let key in formalizedSchema) {
|
|
151
155
|
|
|
156
|
+
key = convenienceNames[key] || key;
|
|
157
|
+
|
|
152
158
|
let validator = validators[key];
|
|
153
159
|
let test = formalizedSchema[key];
|
|
154
160
|
|
package/lib/validate.js
CHANGED
|
@@ -83,7 +83,7 @@ const validateArray = async (data, schema, options, keyPath, validatedData) => {
|
|
|
83
83
|
|
|
84
84
|
if (!Array.isArray(data)) {
|
|
85
85
|
|
|
86
|
-
if (checkBoolValue('
|
|
86
|
+
if (checkBoolValue('autoWrap', schema, options.defaults)) {
|
|
87
87
|
return await validateArray([data], schema, options, keyPath, validatedData);
|
|
88
88
|
} else {
|
|
89
89
|
throw new ValidationError(
|
package/package.json
CHANGED
package/test/validate.js
CHANGED
|
@@ -616,24 +616,24 @@ describe('validate', function() {
|
|
|
616
616
|
});
|
|
617
617
|
});
|
|
618
618
|
});
|
|
619
|
-
describe('
|
|
620
|
-
it('should come back with non-array wrapped in array', () => {
|
|
619
|
+
describe('autoWrap', function() {
|
|
620
|
+
it('should come back with non-array wrapped in array.', () => {
|
|
621
621
|
return expect(isvalid({
|
|
622
622
|
test: true
|
|
623
623
|
}, {
|
|
624
624
|
type: Array,
|
|
625
|
-
|
|
625
|
+
autoWrap: true,
|
|
626
626
|
schema: {
|
|
627
627
|
test: Boolean
|
|
628
628
|
}
|
|
629
629
|
})).to.eventually.be.an('array').and.to.have.property(0).and.to.have.property('test', true);
|
|
630
630
|
});
|
|
631
|
-
it('should come back with type error if
|
|
631
|
+
it('should come back with type error if autoWrap and not matching sub-schema.', () => {
|
|
632
632
|
return expect(isvalid({
|
|
633
633
|
test: 'Not a boolean'
|
|
634
634
|
}, {
|
|
635
635
|
type: Array,
|
|
636
|
-
|
|
636
|
+
autoWrap: true,
|
|
637
637
|
schema: {
|
|
638
638
|
test: Boolean
|
|
639
639
|
}}))
|
|
@@ -641,7 +641,7 @@ describe('validate', function() {
|
|
|
641
641
|
.and.to.be.instanceOf(ValidationError)
|
|
642
642
|
.and.have.property('validator', 'type');
|
|
643
643
|
});
|
|
644
|
-
it('should come back with type error if no
|
|
644
|
+
it('should come back with type error if no autoWrap and matching sub-schema.', () => {
|
|
645
645
|
return expect(isvalid({
|
|
646
646
|
test: true
|
|
647
647
|
}, [{
|
|
@@ -653,8 +653,8 @@ describe('validate', function() {
|
|
|
653
653
|
it('should prioritize concrete over defaults.', () => {
|
|
654
654
|
return expect(isvalid(true, {
|
|
655
655
|
type: Array,
|
|
656
|
-
|
|
657
|
-
}, { defaults: {
|
|
656
|
+
autoWrap: false
|
|
657
|
+
}, { defaults: { autoWrap: true }}))
|
|
658
658
|
.to.eventually.be.rejectedWith('Is not of type array.')
|
|
659
659
|
.and.to.be.instanceOf(ValidationError)
|
|
660
660
|
.and.have.property('validator', 'type');
|