Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/nullable enum validation #312

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `setPriority` super json helper can be used to remove priority

### Fixed
- nullable enum input validation [311](https://github.com/superfaceai/one-sdk-js/issues/311)

## [2.1.0] - 2022-11-16
### Added
- `multipart/form-data` supports array values to define duplicate fields
Expand Down
65 changes: 64 additions & 1 deletion src/core/interpreter/profile-parameter-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,60 @@ describe('ProfileParameterValidator', () => {
).toEqual(true);
});

it('should pass with null', () => {
expect(
parameterValidator.validate({ test: null }, 'input', 'Test').isOk()
).toEqual(true);
});

it('should fail with invalid input', () => {
const result1 = parameterValidator.validate(
{ test: 'none of your business' },
'input',
'Test'
);
expect(checkErrorKind(result1)).toEqual(['enumValue']);
expect(checkErrorPath(result1)).toEqual([['input', 'test']]);
expect(checkErrorContext(result1)).toMatchObject([
{ actual: '"none of your business"' },
]);
const result2 = parameterValidator.validate(
{ test: 7 },
'input',
'Test'
);
expect(checkErrorKind(result2)).toEqual(['enumValue']);
expect(checkErrorPath(result2)).toEqual([['input', 'test']]);
expect(checkErrorContext(result2)).toMatchObject([{ actual: '7' }]);
});
});

describe('AST with non-nullable enum', () => {
const ast = parseProfileFromSource(`
usecase Test safe {
input {
test enum { hello, goodbye }!
}
}
`);
let parameterValidator: ProfileParameterValidator;

beforeEach(() => {
parameterValidator = new ProfileParameterValidator(ast);
});

it('should pass with valid input', () => {
expect(
parameterValidator.validate({ test: 'hello' }, 'input', 'Test').isOk()
).toEqual(true);
});

it('should fail with null', () => {
expect(
parameterValidator.validate({ test: null }, 'input', 'Test').isOk()
).toEqual(false);
});

it('should fail with invalid input', () => {
const result1 = parameterValidator.validate(
{ test: 'none of your business' },
Expand Down Expand Up @@ -375,6 +429,12 @@ describe('ProfileParameterValidator', () => {
).toEqual(true);
});

it('should pass with null', () => {
expect(
parameterValidator.validate({ test: null }, 'input', 'Test').isOk()
).toEqual(true);
});

it('should fail with invalid input', () => {
const result1 = parameterValidator.validate(
{ test: 'none of your business' },
Expand Down Expand Up @@ -410,7 +470,7 @@ describe('ProfileParameterValidator', () => {
parameterValidator = new ProfileParameterValidator(ast);
});

it.each(['A', 'B', 'CC', 'D'])(
it.each(['A', 'B', 'CC', 'D', null])(
'should pass with valid input: %s',
value => {
expect(
Expand Down Expand Up @@ -934,6 +994,9 @@ describe('ProfileParameterValidator', () => {
expect(
parameterValidator.validate({ test: 'c' }, 'result', 'Test').isOk()
).toEqual(true);
expect(
parameterValidator.validate({ test: null }, 'result', 'Test').isOk()
).toEqual(true);
});

it('should fail with invalid input', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/interpreter/profile-parameter-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class ProfileParameterValidator implements ProfileVisitor {
usecase: string
): ValidationFunction {
return (input: unknown): ValidationResult => {
if (input === undefined) {
if (input === undefined || input === null) {
return [true];
}

Expand Down