Skip to content

Commit

Permalink
Merge pull request #14 from marquesVF/bugfix/handle-primitive-number-…
Browse files Browse the repository at this point in the history
…when-nullable

fix(build-object): should ignore types if value is undefined
  • Loading branch information
marquesVF authored Aug 25, 2020
2 parents 573f49a + d8ee637 commit e2bb5a8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,7 @@ Sets a transformation function in the form `<T>(value: unknown) => T` in which a
import { extractObject, BuildTransformer } from 'objectypes'

class SanitizerTransformation implements BuildTransformer<string> {
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('-', '')
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/build-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ export function buildObject<T>(
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()
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.6",
"version": "1.1.7",
"description": "A type-safe library to transform and validate objects",
"files": [
"dist"
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/primitive-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ export class PrimitiveModel {
@Property()
createdAt: Date

@Property({ nullable: true })
someNumber?: number

}

0 comments on commit e2bb5a8

Please sign in to comment.