Skip to content

Commit

Permalink
Merge pull request open-rpc#147 from open-rpc/fix/optional-param-vali…
Browse files Browse the repository at this point in the history
…dation

fix: validate optional params
  • Loading branch information
shanejonas committed Jun 14, 2019
2 parents 48e37b6 + 4feb647 commit ac71a74
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/method-call-validator/method-call-validator.test.ts
Expand Up @@ -51,4 +51,11 @@ describe("MethodCallValidator", () => {
expect(result[0]).toBeInstanceOf(MethodCallParameterValidationError);
});

it("can not error if param is optional", () => {
const example = getExampleSchema() as any;
const methodCallValidator = new MethodCallValidator(example);
const result = methodCallValidator.validate("foo", []);
expect(result).toEqual([]);
});

});
1 change: 1 addition & 0 deletions src/method-call-validator/method-call-validator.ts
Expand Up @@ -67,6 +67,7 @@ export default class MethodCallValidator {
return _.chain(method.params as ContentDescriptorObject[])
.map((param: ContentDescriptorObject, index: number): MethodCallParameterValidationError | undefined => {
if (param.schema === undefined) { return; }
if (!params[index] && !param.required) { return; }

const idForMethod = generateMethodParamId(method, param);
const isValid = this.ajvValidator.validate(idForMethod, params[index]);
Expand Down

0 comments on commit ac71a74

Please sign in to comment.