From 8cca0a3dd0fa4447b0c55611cb162fff7b28eec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20de=20F=2E=20Marques?= Date: Tue, 18 Aug 2020 11:46:34 -0300 Subject: [PATCH 1/2] fix(build-object): apply transformation only if property is not undefined --- lib/build-object.ts | 12 +++++++----- lib/types/transformation.ts | 2 +- test/fixtures/optional-build-model.ts | 11 +++++++++++ test/lib/property-transformation.spec.ts | 9 +++++++++ test/lib/validate-object.spec.ts | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/optional-build-model.ts diff --git a/lib/build-object.ts b/lib/build-object.ts index d063553..0d51d60 100644 --- a/lib/build-object.ts +++ b/lib/build-object.ts @@ -41,11 +41,13 @@ export function buildObject( throw new Error(`Property ${objPropName} type is not assignable to ${expectedType}. Found ${value}`) } - const transformMetadata = transformations - ?.find(metadata => metadata.propertyKey === propertyKey) - if (transformMetadata) { - // TODO improve error handling since it may raise errors in runtine - value = transformMetadata.transformer.transform(value) + if (value !== undefined) { + const transformMetadata = transformations + ?.find(metadata => metadata.propertyKey === propertyKey) + if (transformMetadata) { + // TODO improve error handling since it may raise errors in runtine + value = transformMetadata.transformer.transform(value) + } } if (type && !nullable) { diff --git a/lib/types/transformation.ts b/lib/types/transformation.ts index 80c9c23..b8a5582 100644 --- a/lib/types/transformation.ts +++ b/lib/types/transformation.ts @@ -1,4 +1,4 @@ -export type BuildTransformationFn = (value: unknown) => T +export type BuildTransformationFn = (value: any) => T export interface BuildTransformer { transform: BuildTransformationFn diff --git a/test/fixtures/optional-build-model.ts b/test/fixtures/optional-build-model.ts new file mode 100644 index 0000000..6e4cfeb --- /dev/null +++ b/test/fixtures/optional-build-model.ts @@ -0,0 +1,11 @@ +import { Property, BuildTransformation } from '../../lib' + +export class OptionalBuildModel { + + @BuildTransformation({ + transform: (value: string) => value.replace(/\./g, '') + }) + @Property({ nullable: true }) + code?: string + +} diff --git a/test/lib/property-transformation.spec.ts b/test/lib/property-transformation.spec.ts index 5453ad0..2443541 100644 --- a/test/lib/property-transformation.spec.ts +++ b/test/lib/property-transformation.spec.ts @@ -1,5 +1,6 @@ import { Transformable } from '../fixtures/transformable' import { extractObject, buildObject } from '../../lib' +import { OptionalBuildModel } from '../fixtures/optional-build-model' describe('property transformation', () => { const extractedObject = { @@ -45,5 +46,13 @@ describe('property transformation', () => { expect(result).toThrowError() }) }) + + describe('when typed object has a transformation for a optional property', () => { + it('should not execute the transformation if property is undefined', () => { + const builder = () => buildObject(OptionalBuildModel, { }) + + expect(builder).not.toThrowError() + }) + }) }) }) diff --git a/test/lib/validate-object.spec.ts b/test/lib/validate-object.spec.ts index 17a33a7..4b35b20 100644 --- a/test/lib/validate-object.spec.ts +++ b/test/lib/validate-object.spec.ts @@ -3,6 +3,7 @@ import { NestedModel } from '../fixtures/nested-model' import { validateObject } from '../../lib' import { VendorModel } from '../fixtures/vendor-model' import { ComplexModel } from '../fixtures/complex-mode' +import { PrimitiveModel } from '../fixtures/primitive-model' describe('validateObject method', () => { describe('when there is a valid JSON object', () => { @@ -95,4 +96,19 @@ describe('validateObject method', () => { expect(typeErrors).toHaveLength(0) }) }) + + describe('when model has primitive types', () => { + describe('when json object has unexpected property types but convertable', () => { + const model = { + counter: '10', + createdAt: '2020-08-14T17:34:42.475Z' + } + + it('should not return any type error', () => { + const { typeErrors } = validateObject(PrimitiveModel, model) + + expect(typeErrors).toHaveLength(0) + }) + }) + }) }) From dc3e802c48cf03df6ae34377fbdd3d40fe73ab94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20de=20F=2E=20Marques?= Date: Tue, 18 Aug 2020 11:47:17 -0300 Subject: [PATCH 2/2] chore: version 1.1.5 --- 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 5c9b034..87300bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "objectypes", - "version": "1.1.4", + "version": "1.1.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5c8b212..943c573 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "objectypes", - "version": "1.1.4", + "version": "1.1.5", "description": "A type-safe library to transform and validate objects", "files": [ "dist"