Skip to content

Commit

Permalink
Merge pull request #12 from marquesVF/fix/build-object-transformation…
Browse files Browse the repository at this point in the history
…-when-property-is-undefined

Fix/build object transformation when property is undefined
  • Loading branch information
marquesVF authored Aug 18, 2020
2 parents c49b774 + dc3e802 commit 9e331c3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/build-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export function buildObject<T>(
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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/types/transformation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type BuildTransformationFn<T> = (value: unknown) => T
export type BuildTransformationFn<T> = (value: any) => T

export interface BuildTransformer<T> {
transform: BuildTransformationFn<T>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/optional-build-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Property, BuildTransformation } from '../../lib'

export class OptionalBuildModel {

@BuildTransformation({
transform: (value: string) => value.replace(/\./g, '')
})
@Property({ nullable: true })
code?: string

}
9 changes: 9 additions & 0 deletions test/lib/property-transformation.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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()
})
})
})
})
16 changes: 16 additions & 0 deletions test/lib/validate-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
})
})

0 comments on commit 9e331c3

Please sign in to comment.