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
This commit is contained in:
Rikard Teodorsson
2021-04-19 07:22:23 +02:00
committed by GitHub
parent 25690ad515
commit 64fc04cc26
2 changed files with 122 additions and 8 deletions

View File

@@ -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',

View File

@@ -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));
}
}
}