Skip to content

Commit d842842

Browse files
committed
Add specail case when no required condition found
When conditions are added but not "required" then they will generally fail for an undefined value. This should not be the case. In other words, we need a specail exit before running conditions
1 parent be17648 commit d842842

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/conditions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function required(value) {
1313
return true;
1414
};
1515
required.priority = 3; // needed for ordering
16+
required.hasRequiredCondition = true// special case for shortcoming in validation
1617

1718
/**
1819
*

src/tests/validation.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ describe('shared/type-validation', () => {
113113
});
114114

115115
describe('isNumeric', () => {
116+
116117
it('does not return an error if the value is a number', () => {
117118
assert.equal(types.isNumeric(1), true);
118119
});
@@ -196,6 +197,13 @@ describe('shared/type-validation', () => {
196197
);
197198
});
198199

200+
it('returns true if the value is undefined and not required', async () => {
201+
assert.equal(
202+
await types.isNumeric.and(conditions.range(0, 100))(undefined),
203+
true
204+
);
205+
});
206+
199207
it('can still can be required', async () => {
200208
assert.equal(await types.isNumeric.and(
201209
conditions.range(0, 10),
@@ -278,12 +286,19 @@ describe('shared/type-validation', () => {
278286
assert.equal(result, 'string does not match');
279287
});
280288

289+
it('does not return an error if the value is undefined', async () => {
290+
const custom = (v)=>(v === "hello world" ? true : "string does not match")
291+
const result = await types.isCustom(custom)(undefined)
292+
assert.equal(result, true);
293+
});
294+
281295
describe('with required', () => {
282296
it('runs a custom function and passes', async () => {
283297
const custom = (v)=>(v === "hello world" ? true : "string does not match")
284298
const result = await types.isCustom(custom).and(conditions.required)("hello world")
285299
assert.equal(result, true);
286300
});
301+
287302
it('returns an error if the value is undefined', async () => {
288303
const custom = (v)=>(v === "hello world" ? true : "string does not match")
289304
const result = await types.isCustom(custom).and(conditions.required)(undefined)

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ function attachOptions(shim /*, validOptions*/) {
66
let result;
77
shim.and = (...options) => {
88
const validate = async (value, ...args) => {
9+
910
result = await shim(value, ...args)
1011
if (result !== true) {
1112
return result
1213
}
1314

15+
let required = options.filter(v=>v && v.hasRequiredCondition == true);
16+
if(required.length == 0 && value === undefined) {
17+
// special case for undefined values so they avoid running further conditions.
18+
return true;
19+
}
20+
1421
// ensures high priority conditions are ran first
1522
// so we resolve things like required before nested objects
1623
options.sort((v1, v2)=>{

0 commit comments

Comments
 (0)