From 64fc04cc2688f3ce1ff7cf98fb9c522253831f66 Mon Sep 17 00:00:00 2001 From: Rikard Teodorsson <9367038+hej2010@users.noreply.github.com> Date: Mon, 19 Apr 2021 07:22:23 +0200 Subject: [PATCH] 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 --- spec/CloudCode.Validator.spec.js | 111 +++++++++++++++++++++++++++++++ src/triggers.js | 19 +++--- 2 files changed, 122 insertions(+), 8 deletions(-) diff --git a/spec/CloudCode.Validator.spec.js b/spec/CloudCode.Validator.spec.js index a62fc340..b5d51345 100644 --- a/spec/CloudCode.Validator.spec.js +++ b/spec/CloudCode.Validator.spec.js @@ -355,6 +355,117 @@ describe('cloud validator', () => { }); }); + it('set params not-required options data', done => { + Parse.Cloud.define( + 'hello', + req => { + expect(req.params.data).toBe('abc'); + return 'Hello world!'; + }, + { + fields: { + data: { + type: String, + required: false, + options: s => { + return s.length >= 4 && s.length <= 50; + }, + error: 'Validation failed. Expected length of data to be between 4 and 50.', + }, + }, + } + ); + Parse.Cloud.run('hello', { data: 'abc' }) + .then(() => { + fail('function should have failed.'); + }) + .catch(error => { + expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR); + expect(error.message).toEqual( + 'Validation failed. Expected length of data to be between 4 and 50.' + ); + done(); + }); + }); + + it('set params not-required type', done => { + Parse.Cloud.define( + 'hello', + req => { + expect(req.params.data).toBe(null); + return 'Hello world!'; + }, + { + fields: { + data: { + type: String, + required: false, + }, + }, + } + ); + Parse.Cloud.run('hello', { data: null }) + .then(() => { + fail('function should have failed.'); + }) + .catch(error => { + expect(error.code).toEqual(Parse.Error.VALIDATION_ERROR); + expect(error.message).toEqual('Validation failed. Invalid type for data. Expected: string'); + done(); + }); + }); + + it('set params not-required options', done => { + Parse.Cloud.define( + 'hello', + () => { + return 'Hello world!'; + }, + { + fields: { + data: { + type: String, + required: false, + options: s => { + return s.length >= 4 && s.length <= 50; + }, + }, + }, + } + ); + Parse.Cloud.run('hello', {}) + .then(() => { + done(); + }) + .catch(() => { + fail('function should not have failed.'); + }); + }); + + it('set params not-required no-options', done => { + Parse.Cloud.define( + 'hello', + () => { + return 'Hello world!'; + }, + { + fields: { + data: { + type: String, + required: false, + }, + }, + } + ); + Parse.Cloud.run('hello', {}) + .then(() => { + done(); + }) + .catch(() => { + fail('function should not have failed.'); + }); + }); + it('set params option', done => { Parse.Cloud.define( 'hello', diff --git a/src/triggers.js b/src/triggers.js index 0a9e2342..cdd43531 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -724,15 +724,18 @@ async function builtInTriggerValidator(options, request, auth) { if (opt.required) { requiredParam(key); } - if (opt.type) { - const type = getType(opt.type); - const valType = Array.isArray(val) ? 'array' : typeof val; - if (valType !== type) { - throw `Validation failed. Invalid type for ${key}. Expected: ${type}`; + const optional = !opt.required && val === undefined; + if (!optional) { + if (opt.type) { + const type = getType(opt.type); + const valType = Array.isArray(val) ? 'array' : typeof val; + if (valType !== type) { + throw `Validation failed. Invalid type for ${key}. Expected: ${type}`; + } + } + if (opt.options) { + optionPromises.push(validateOptions(opt, key, val)); } - } - if (opt.options) { - optionPromises.push(validateOptions(opt, key, val)); } } }