From af1140fac9e17fe76f561d108bbeed50ea73a5fa Mon Sep 17 00:00:00 2001 From: Yaraslau Stsetskevich Date: Fri, 15 Oct 2021 12:34:33 +0300 Subject: [PATCH 1/2] feat: introduce asNullable --- sanitizers/src/asNullable.ts | 8 ++++++++ sanitizers/src/index.ts | 1 + sanitizers/test/asNullable.test.ts | 33 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 sanitizers/src/asNullable.ts create mode 100644 sanitizers/test/asNullable.test.ts diff --git a/sanitizers/src/asNullable.ts b/sanitizers/src/asNullable.ts new file mode 100644 index 0000000..e5fb687 --- /dev/null +++ b/sanitizers/src/asNullable.ts @@ -0,0 +1,8 @@ +import { Sanitizer, Result } from "."; + +export const asNullable = (sanitizer: Sanitizer) : Sanitizer => + (value, path) => value === null + ? Result.ok(null) + : value === undefined + ? Result.error([{ path: 'path', expected: 'not undefined' }]) + : sanitizer(value, path) diff --git a/sanitizers/src/index.ts b/sanitizers/src/index.ts index 7353099..b79cfd4 100644 --- a/sanitizers/src/index.ts +++ b/sanitizers/src/index.ts @@ -8,6 +8,7 @@ export { asFlatMapped } from './asFlatMapped' export { asInteger } from './asInteger' export { asMapped } from './asMapped' export { asMatching } from './asMatching' +export { asNullable } from './asNullable' export { asNumber } from './asNumber' export { asObject } from './asObject' export { asOptional } from './asOptional' diff --git a/sanitizers/test/asNullable.test.ts b/sanitizers/test/asNullable.test.ts new file mode 100644 index 0000000..9eb57ac --- /dev/null +++ b/sanitizers/test/asNullable.test.ts @@ -0,0 +1,33 @@ +import { expect } from 'chai' +import { asString, asNullable, Result } from '../src'; + + +describe('asNullable', () => { + it('sanitizes using the nested sanitizer', async () => { + const asNullableString = asNullable(asString) + const result = asNullableString('abc', '') + expect(result).to.deep.equal(Result.ok('abc')) + }) + + it('returns nested sanitizer errors', async () => { + const asNullableString = asNullable(asString) + const result = asNullableString(false, 'path') + expect(result).to.deep.equal( + Result.error([{ path: 'path', expected: 'string' }]) + ) + }) + + it('sanitizes undefined', async () => { + const asNullableString = asNullable(asString) + const result = asNullableString(undefined, 'path') + expect(result).to.deep.equal( + Result.error([{ path: 'path', expected: 'not undefined' }]) + ) + }) + + it('sanitizes null', async () => { + const asNullableString = asNullable(asString) + const result = asNullableString(null, 'path') + expect(result).to.deep.equal(Result.ok(null)) + }) +}); \ No newline at end of file From b2e66b408385b9cc674efdc95770bed898822f8f Mon Sep 17 00:00:00 2001 From: Dmytro Maretskyi <35851437+Marik-D@users.noreply.github.com> Date: Fri, 15 Oct 2021 15:05:02 +0300 Subject: [PATCH 2/2] Update sanitizers/src/asNullable.ts --- sanitizers/src/asNullable.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanitizers/src/asNullable.ts b/sanitizers/src/asNullable.ts index e5fb687..59f5a1f 100644 --- a/sanitizers/src/asNullable.ts +++ b/sanitizers/src/asNullable.ts @@ -4,5 +4,5 @@ export const asNullable = (sanitizer: Sanitizer) : Sanitizer => (value, path) => value === null ? Result.ok(null) : value === undefined - ? Result.error([{ path: 'path', expected: 'not undefined' }]) + ? Result.error([{ path, expected: 'not undefined' }]) : sanitizer(value, path)