Skip to content

Commit

Permalink
Cloud Code validator issue with required: false (#7353)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hej2010 authored Apr 19, 2021
1 parent 25690ad commit 64fc04c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
111 changes: 111 additions & 0 deletions spec/CloudCode.Validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
19 changes: 11 additions & 8 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down

0 comments on commit 64fc04c

Please sign in to comment.