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

Commit

Permalink
Merge 24008ee into 8d90843
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Nov 2, 2019
2 parents 8d90843 + 24008ee commit 472d222
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function isEmpty(result: Result) {
function requiredValidationVisitor(i: VisitorInvocation): Result {
const result = i.proceed()
if (isEmpty(result) && !isOptional(i) && !isPartial(i))
return Result.error(i.path, `Required`)
return Result.error(i.value, i.path, `Required`)
else
return result;
}
Expand All @@ -77,7 +77,7 @@ function validatorVisitor(i: VisitorInvocation): Result {
if (msg) messages.push(msg)
}
}
return messages.length > 0 ? Result.error(i.path, messages) : result
return messages.length > 0 ? Result.error(i.value, i.path, messages) : result
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace Result {
export function create(value: any): Result {
return { value }
}
export function error(path: string, message: string | string[]): Result {
return { value: undefined, issues: [{ path, messages: Array.isArray(message) ? message : [message] }] }
export function error(value: any, path: string, message: string | string[]): Result {
return { value, issues: [{ path, messages: Array.isArray(message) ? message : [message] }] }
}
}

Expand Down Expand Up @@ -68,14 +68,14 @@ function unableToConvert(value: {}, type: string) {
function primitiveVisitor(value: {}, ast: PrimitiveNode, opt: VisitorOption): Result {
const result = ast.converter(value)
if (result === undefined)
return Result.error(opt.path, unableToConvert(value, ast.type.name))
return Result.error(value, opt.path, unableToConvert(value, ast.type.name))
else
return Result.create(result)
}

function arrayVisitor(value: {}[], ast: ArrayNode, opt: VisitorOption): Result {
const newValues = opt.guessArrayElement && !Array.isArray(value) ? [value] : value
if (!Array.isArray(newValues)) return Result.error(opt.path, unableToConvert(value, `Array<${ast.type.name}>`))
if (!Array.isArray(newValues)) return Result.error(value, opt.path, unableToConvert(value, `Array<${ast.type.name}>`))
const result: any[] = []
const errors: ResultMessages[] = []
for (let i = 0; i < newValues.length; i++) {
Expand All @@ -95,7 +95,7 @@ function arrayVisitor(value: {}[], ast: ArrayNode, opt: VisitorOption): Result {

function objectVisitor(value: any, ast: ObjectNode, opt: VisitorOption): Result {
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean")
return Result.error(opt.path, unableToConvert(value, ast.type.name))
return Result.error(value, opt.path, unableToConvert(value, ast.type.name))
const instance = Object.create(ast.type.prototype)
const meta = reflect(ast.type)
const errors: ResultMessages[] = []
Expand Down
19 changes: 17 additions & 2 deletions test/__snapshots__/validation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Object {
],
"value": Array [
1,
undefined,
null,
3,
],
}
Expand Down Expand Up @@ -364,6 +364,7 @@ Object {
"birthday": 2018-02-02,
"deceased": true,
"id": 200,
"name": "",
},
}
`;
Expand Down Expand Up @@ -436,6 +437,20 @@ Object {
}
`;

exports[`Optional & Partial Validation Primitive Type Validation Should give proper info if provided wrong type of value 1`] = `
Object {
"issues": Array [
Object {
"messages": Array [
"Unable to convert \\"hula\\" into Number",
],
"path": "data",
},
],
"value": "hula",
}
`;

exports[`Optional & Partial Validation Primitive Type Validation Should valid if optional 1`] = `
Object {
"value": null,
Expand All @@ -452,7 +467,7 @@ Object {
"path": "data",
},
],
"value": undefined,
"value": null,
}
`;

Expand Down
17 changes: 13 additions & 4 deletions test/__snapshots__/visitor.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ Object {
"path": "",
},
],
"value": undefined,
"value": "12",
}
`;

Expand All @@ -403,6 +403,8 @@ Object {
},
],
"value": AnimalClass {
"age": "12",
"id": "12",
"name": "Mimi",
},
}
Expand All @@ -425,11 +427,15 @@ Object {
},
],
"value": Array [
Tag {},
Tag {
"age": "12",
},
Tag {
"age": 40,
},
Tag {},
Tag {
"age": "12",
},
],
}
`;
Expand All @@ -451,8 +457,11 @@ Object {
},
],
"value": AnimalClass {
"id": "12",
"name": "Mimi",
"tag": Tag {},
"tag": Tag {
"age": "12",
},
},
}
`;
4 changes: 2 additions & 2 deletions test/number-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe("Number Converter", () => {
const result = convert(undefined)
expect(result.value).toBeUndefined()
})
it("Should return undefined if provided empty string", () => {
it("Should validate if provided empty string", () => {
const result = convert("")
expect(result.value).toBeUndefined()
expect(result.issues).toEqual([{ path: "", messages: [`Unable to convert "" into Number`] }])
})
it("Should not convert string", () => {
const result = convert("Hello")
Expand Down
7 changes: 7 additions & 0 deletions test/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe("Optional & Partial Validation", () => {
})
expect(result).toMatchSnapshot()
})
it("Should give proper info if provided wrong type of value", () =>{
const result = validate("hula", {
path: "data",
type: Number,
})
expect(result).toMatchSnapshot()
})
it("Should valid if optional", () => {
const result = validate(null, {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: OptionalValidator }],
Expand Down
4 changes: 2 additions & 2 deletions test/visitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ describe("Visitor", () => {

const olderThanEightTeen = (i: VisitorInvocation) => {
if (i.type === Number && i.value < 18)
return Result.error(i.path, "Must be older than 18")
return Result.error(i.value, i.path, "Must be older than 18")
else
return i.proceed()
}

it("Should be able to create result of multiple messages", () => {
const result = Result.error("path", ["Hello", "world"])
const result = Result.error(undefined, "path", ["Hello", "world"])
expect(result).toMatchSnapshot()
})

Expand Down

0 comments on commit 472d222

Please sign in to comment.