From fbe8003a1442a8e41afef74b28bab8e68121cfc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Marques?= Date: Tue, 25 Aug 2020 11:11:45 -0300 Subject: [PATCH 1/2] fix(build-object): should ignore types if value is undefined --- README.md | 8 +------- lib/build-object.ts | 6 +++++- test/fixtures/primitive-model.ts | 3 +++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 94ad159..2194a71 100644 --- a/README.md +++ b/README.md @@ -210,13 +210,7 @@ Sets a transformation function in the form `(value: unknown) => T` in which a import { extractObject, BuildTransformer } from 'objectypes' class SanitizerTransformation implements BuildTransformer { - transform(value: unknown): Date { - if (typeof value !== 'string') { - throw new Error( - `'${value}' has not a valid value. Expected a string.` - ) - } - + transform(value: string): string { return value.replace('-', '') } } diff --git a/lib/build-object.ts b/lib/build-object.ts index 0d51d60..54001e7 100644 --- a/lib/build-object.ts +++ b/lib/build-object.ts @@ -65,7 +65,11 @@ export function buildObject( return targetObj } -function castValue(expectedType: string, value: any): any { +function castValue(expectedType: string, value?: any): any { + if (value === undefined) { + return value + } + if (expectedType === 'Number') { if (isNaN(value)) { throw new Error() diff --git a/test/fixtures/primitive-model.ts b/test/fixtures/primitive-model.ts index 5259aa9..1a89b12 100644 --- a/test/fixtures/primitive-model.ts +++ b/test/fixtures/primitive-model.ts @@ -8,4 +8,7 @@ export class PrimitiveModel { @Property() createdAt: Date + @Property({ nullable: true }) + someNumber?: number + } From d8ee6372e0063cec4071ee1956cb741c2827b393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Marques?= Date: Tue, 25 Aug 2020 11:15:16 -0300 Subject: [PATCH 2/2] chore: version 1.1.7 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 938284c..b4dab75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "objectypes", - "version": "1.1.6", + "version": "1.1.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 44a5515..3a66470 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "objectypes", - "version": "1.1.6", + "version": "1.1.7", "description": "A type-safe library to transform and validate objects", "files": [ "dist"