diff --git a/CHANGELOG.md b/CHANGELOG.md index aa8fafba..020e141f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/core/interpreter/profile-parameter-validator.test.ts b/src/core/interpreter/profile-parameter-validator.test.ts index c28785d3..4775a7c7 100644 --- a/src/core/interpreter/profile-parameter-validator.test.ts +++ b/src/core/interpreter/profile-parameter-validator.test.ts @@ -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' }, @@ -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' }, @@ -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( @@ -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', () => { diff --git a/src/core/interpreter/profile-parameter-validator.ts b/src/core/interpreter/profile-parameter-validator.ts index bc243ee5..a78f8e43 100644 --- a/src/core/interpreter/profile-parameter-validator.ts +++ b/src/core/interpreter/profile-parameter-validator.ts @@ -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]; }