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:
committed by
GitHub
parent
25690ad515
commit
64fc04cc26
@@ -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 => {
|
it('set params option', done => {
|
||||||
Parse.Cloud.define(
|
Parse.Cloud.define(
|
||||||
'hello',
|
'hello',
|
||||||
|
|||||||
@@ -724,15 +724,18 @@ async function builtInTriggerValidator(options, request, auth) {
|
|||||||
if (opt.required) {
|
if (opt.required) {
|
||||||
requiredParam(key);
|
requiredParam(key);
|
||||||
}
|
}
|
||||||
if (opt.type) {
|
const optional = !opt.required && val === undefined;
|
||||||
const type = getType(opt.type);
|
if (!optional) {
|
||||||
const valType = Array.isArray(val) ? 'array' : typeof val;
|
if (opt.type) {
|
||||||
if (valType !== type) {
|
const type = getType(opt.type);
|
||||||
throw `Validation failed. Invalid type for ${key}. Expected: ${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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user