Skip to content

Commit 64fc04c

Browse files
authored
Cloud Code validator issue with required: false (#7353)
* Only check the type of a parameter in the validator when the parameter is set to required or is not null * Added test cases. Don't check type or options if required=false and no default value is set * Added test cases. Don't check type or options if required=false and no default value is set * Update const optional
1 parent 25690ad commit 64fc04c

File tree

2 files changed

+122
-8
lines changed

2 files changed

+122
-8
lines changed

spec/CloudCode.Validator.spec.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,117 @@ describe('cloud validator', () => {
355355
});
356356
});
357357

358+
it('set params not-required options data', done => {
359+
Parse.Cloud.define(
360+
'hello',
361+
req => {
362+
expect(req.params.data).toBe('abc');
363+
return 'Hello world!';
364+
},
365+
{
366+
fields: {
367+
data: {
368+
type: String,
369+
required: false,
370+
options: s => {
371+
return s.length >= 4 && s.length <= 50;
372+
},
373+
error: 'Validation failed. Expected length of data to be between 4 and 50.',
374+
},
375+
},
376+
}
377+
);
378+
Parse.Cloud.run('hello', { data: 'abc' })
379+
.then(() => {
380+
fail('function should have failed.');
381+
})
382+
.catch(error => {
383+
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
384+
expect(error.message).toEqual(
385+
'Validation failed. Expected length of data to be between 4 and 50.'
386+
);
387+
done();
388+
});
389+
});
390+
391+
it('set params not-required type', done => {
392+
Parse.Cloud.define(
393+
'hello',
394+
req => {
395+
expect(req.params.data).toBe(null);
396+
return 'Hello world!';
397+
},
398+
{
399+
fields: {
400+
data: {
401+
type: String,
402+
required: false,
403+
},
404+
},
405+
}
406+
);
407+
Parse.Cloud.run('hello', { data: null })
408+
.then(() => {
409+
fail('function should have failed.');
410+
})
411+
.catch(error => {
412+
expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR);
413+
expect(error.message).toEqual('Validation failed. Invalid type for data. Expected: string');
414+
done();
415+
});
416+
});
417+
418+
it('set params not-required options', done => {
419+
Parse.Cloud.define(
420+
'hello',
421+
() => {
422+
return 'Hello world!';
423+
},
424+
{
425+
fields: {
426+
data: {
427+
type: String,
428+
required: false,
429+
options: s => {
430+
return s.length >= 4 && s.length <= 50;
431+
},
432+
},
433+
},
434+
}
435+
);
436+
Parse.Cloud.run('hello', {})
437+
.then(() => {
438+
done();
439+
})
440+
.catch(() => {
441+
fail('function should not have failed.');
442+
});
443+
});
444+
445+
it('set params not-required no-options', done => {
446+
Parse.Cloud.define(
447+
'hello',
448+
() => {
449+
return 'Hello world!';
450+
},
451+
{
452+
fields: {
453+
data: {
454+
type: String,
455+
required: false,
456+
},
457+
},
458+
}
459+
);
460+
Parse.Cloud.run('hello', {})
461+
.then(() => {
462+
done();
463+
})
464+
.catch(() => {
465+
fail('function should not have failed.');
466+
});
467+
});
468+
358469
it('set params option', done => {
359470
Parse.Cloud.define(
360471
'hello',

src/triggers.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,18 @@ async function builtInTriggerValidator(options, request, auth) {
724724
if (opt.required) {
725725
requiredParam(key);
726726
}
727-
if (opt.type) {
728-
const type = getType(opt.type);
729-
const valType = Array.isArray(val) ? 'array' : typeof val;
730-
if (valType !== type) {
731-
throw `Validation failed. Invalid type for ${key}. Expected: ${type}`;
727+
const optional = !opt.required && val === undefined;
728+
if (!optional) {
729+
if (opt.type) {
730+
const type = getType(opt.type);
731+
const valType = Array.isArray(val) ? 'array' : typeof val;
732+
if (valType !== type) {
733+
throw `Validation failed. Invalid type for ${key}. Expected: ${type}`;
734+
}
735+
}
736+
if (opt.options) {
737+
optionPromises.push(validateOptions(opt, key, val));
732738
}
733-
}
734-
if (opt.options) {
735-
optionPromises.push(validateOptions(opt, key, val));
736739
}
737740
}
738741
}

0 commit comments

Comments
 (0)