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

Commit

Permalink
fix: Required validator priority should be higher than other
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Nov 1, 2019
1 parent 48ef89b commit 9a6d7d2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
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 === "" || result.value === undefined || result.value === null) && result.issues === undefined
return (result.value === "" || result.value === undefined || result.value === null)
}

// --------------------------------------------------------------------- //
Expand Down
74 changes: 37 additions & 37 deletions test/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import reflect, { decorateMethod } from "tinspector"

import {
convert,
OptionalValidator,
PartialValidator,
requiredValidationVisitor,
val,
validate,
ValidatorDecorator,
validatorVisitor,
VisitorInvocation,
Result,
} from "../src"
Expand Down Expand Up @@ -52,27 +49,24 @@ describe("Optional & Partial Validation", () => {
describe("Primitive Type Validation", () => {

it("Should validate undefined", () => {
const result = convert(undefined, {
const result = validate(undefined, {
path: "data",
type: Number,
visitors: [requiredValidationVisitor]
type: Number
})
expect(result).toMatchSnapshot()
})
it("Should validate null", () => {
const result = convert(null, {
const result = validate(null, {
path: "data",
type: Number,
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
it("Should valid if optional", () => {
const result = convert(null, {
const result = validate(null, {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: OptionalValidator }],
path: "data",
type: Number,
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
Expand All @@ -90,58 +84,55 @@ describe("Optional & Partial Validation", () => {
) { }
}

const option = { type: AnimalClass, visitors: [requiredValidationVisitor] }
const option = { type: AnimalClass, }

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

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

it("Should validate empty string property", () => {
const result = convert({ id: "200", name: "", deceased: "ON", birthday: "2018-2-2" }, { ...option })
const result = validate({ 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 })
const result = validate({ id: "123", name: "Mimi", deceased: "ON", birthday: null }, { ...option })
expect(result).toMatchSnapshot()
})

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

describe("Array of Primitive Type Validation", () => {

it("Should validate undefined", () => {
const result = convert(["1", undefined, "3"], {
const result = validate(["1", undefined, "3"], {
path: "data",
type: [Number],
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
it("Should validate null", () => {
const result = convert(["1", null, "3"], {
const result = validate(["1", null, "3"], {
path: "data",
type: [Number],
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
it("Should valid if optional", () => {
const result = convert(["1", null, "3"], {
const result = validate(["1", null, "3"], {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: OptionalValidator }],
path: "data",
type: [Number],
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
Expand All @@ -159,20 +150,20 @@ describe("Optional & Partial Validation", () => {
) { }
}

const option = { type: [AnimalClass], visitors: [requiredValidationVisitor] }
const option = { type: [AnimalClass], }

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

it("Should validate null property", () => {
const result = convert([undefined, { id: null, name: "Mimi", deceased: "ON", birthday: "2018-2-2" }], { ...option })
const result = validate([undefined, { id: null, name: "Mimi", deceased: "ON", birthday: "2018-2-2" }], { ...option })
expect(result).toMatchSnapshot()
})

it("Should valid if optional", () => {
const result = convert([undefined, { id: "123", name: "Mimi", deceased: "ON", birthday: null }], { ...option, decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: OptionalValidator }] })
const result = validate([undefined, { id: "123", name: "Mimi", deceased: "ON", birthday: null }], { ...option, decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: OptionalValidator }] })
expect(result).toMatchSnapshot()
})
})
Expand All @@ -198,25 +189,25 @@ describe("Optional & Partial Validation", () => {
) { }
}

const option = { type: AnimalClass, visitors: [requiredValidationVisitor] }
const option = { type: AnimalClass, }

it("Should validate undefined property", () => {
const result = convert({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: undefined, name: "The Tag" } }, { ...option })
const result = validate({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: undefined, name: "The Tag" } }, { ...option })
expect(result).toMatchSnapshot()
})

it("Should validate null property", () => {
const result = convert({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: null, name: "The Tag" } }, { ...option })
const result = validate({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: null, name: "The Tag" } }, { ...option })
expect(result).toMatchSnapshot()
})

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

it("Should valid if provided undefined optional", () => {
const result = convert({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: "123", name: undefined } }, { ...option })
const result = validate({ id: "123", name: "Mimi", deceased: "ON", birthday: "2018-2-2", tag: { id: "123", name: undefined } }, { ...option })
expect(result).toMatchSnapshot()
})
})
Expand All @@ -232,11 +223,10 @@ describe("Optional & Partial Validation", () => {
public birthday: Date
) { }
}
const result = convert({ id: "123" }, {
const result = validate({ id: "123" }, {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: PartialValidator }],
path: "data",
type: AnimalClass,
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
Expand All @@ -252,11 +242,10 @@ describe("Optional & Partial Validation", () => {
public birthday: Date
) { }
}
const result = convert({ id: "123" }, {
const result = validate({ id: "123" }, {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: PartialValidator }],
path: "data",
type: AnimalClass,
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
Expand All @@ -271,11 +260,10 @@ describe("Optional & Partial Validation", () => {
public birthday: Date
) { }
}
const result = convert(undefined, {
const result = validate(undefined, {
decorators: [<ValidatorDecorator>{ type: "tc:validator", validator: PartialValidator }],
path: "data",
type: AnimalClass,
visitors: [requiredValidationVisitor]
})
expect(result).toMatchSnapshot()
})
Expand All @@ -296,6 +284,18 @@ describe("Validator", () => {
expect(result).toMatchObject({ value: { property: "lorem.ipsum@gmail.com" } })
})

it("Should prioritize Required validator than other validator", () => {
@reflect.parameterProperties()
class Dummy {
constructor(
@val.email()
public property: string
) { }
}
const result = validate({ property: "" }, { type: Dummy, })
expect(result.issues).toMatchObject([{ path: "property", messages: ["Required"] }])
})

it("Should able combine optional and email", () => {
@reflect.parameterProperties()
class Dummy {
Expand Down

0 comments on commit 9a6d7d2

Please sign in to comment.