Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Merge branch 'upgrage-devs' of github.com:plumier/typedconverter into…
Browse files Browse the repository at this point in the history
… upgrage-devs
  • Loading branch information
ktutnik committed Nov 1, 2019
2 parents 8acb92d + 86fb8a1 commit 0854b61
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ function booleanConverter(rawValue: {}) {

function numberConverter(rawValue: {}) {
const value = safeToString(rawValue)
if (value === "") return
const result = Number(value)
return isNaN(result) ? undefined : result
if (isNaN(result)) return
return result
}

function dateConverter(rawValue: {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function isPartial(i: VisitorInvocation) {
}

function isEmpty(result: Result) {
return (result.value === undefined || result.value === null) && result.issues === undefined
return (result.value === "" || result.value === undefined || result.value === null) && result.issues === undefined
}

// --------------------------------------------------------------------- //
Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/validation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,24 @@ Object {
}
`;

exports[`Optional & Partial Validation Object Validator Should validate empty string property 1`] = `
Object {
"issues": Array [
Object {
"messages": Array [
"Required",
],
"path": "name",
},
],
"value": AnimalClass {
"birthday": 2018-02-02,
"deceased": true,
"id": 200,
},
}
`;

exports[`Optional & Partial Validation Object Validator Should validate null property 1`] = `
Object {
"issues": Array [
Expand Down
4 changes: 4 additions & 0 deletions test/number-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe("Number Converter", () => {
const result = convert(undefined)
expect(result.value).toBeUndefined()
})
it("Should return undefined if provided empty string", () => {
const result = convert("")
expect(result.value).toBeUndefined()
})
it("Should not convert string", () => {
const result = convert("Hello")
expect(result.issues).toEqual([{ path: "", messages: [`Unable to convert "Hello" into Number`] }])
Expand Down
5 changes: 5 additions & 0 deletions test/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ describe("Optional & Partial Validation", () => {
expect(result).toMatchSnapshot()
})

it("Should validate empty string property", () => {
const result = convert({ id: "200", name: "", deceased: "ON", birthday: "2018-2-2" }, { ...option })
expect(result).toMatchSnapshot()
})

it("Should valid if provided null optional", () => {
const result = convert({ id: "123", name: "Mimi", deceased: "ON", birthday: null }, { ...option })
expect(result).toMatchSnapshot()
Expand Down

0 comments on commit 0854b61

Please sign in to comment.