Created
May 16, 2019 08:04
-
-
Save akkunchoi/fe61386e12f79eb92d4f138208722d97 to your computer and use it in GitHub Desktop.
class-validator groups, always
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "es6-shim"; | |
import {IsNotEmpty, ValidateIf, IsOptional, Equals} from "../src/decorator/decorators"; | |
import {Validator} from "../src/validation/Validator"; | |
import {ValidatorOptions} from "../src/validation/ValidatorOptions"; | |
import {expect, should, use } from "chai"; | |
import * as chaiAsPromised from "chai-as-promised"; | |
import { MinLength, ValidateNested } from "../src"; | |
should(); | |
use(chaiAsPromised); | |
// ------------------------------------------------------------------------- | |
// Setup | |
// ------------------------------------------------------------------------- | |
const validator = new Validator(); | |
describe.only("labo", () => { | |
describe("", () => { | |
class X { | |
@IsNotEmpty({ always: true }) | |
name: string; | |
} | |
it("alwaysの場合、グループ指定してもvalidation実行する", async () => { | |
const x = new X(); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class X { | |
@IsNotEmpty({ always: false }) | |
name: string; | |
} | |
it("always=falseの場合、グループにマッチで実行する", async () => { | |
const x = new X(); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class X { | |
@IsNotEmpty({ groups: ["c"] }) | |
name: string; | |
} | |
it("group指定した場合、いずれかにマッチで実行する", async () => { | |
const x = new X(); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class X { | |
@IsNotEmpty({ groups: ["c", "d"] }) | |
name: string; | |
} | |
it("group指定した場合、いずれかにマッチで実行する。複数でも同様", async () => { | |
const x = new X(); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class X { | |
@IsNotEmpty({ groups: ["c", "d"], always: true }) | |
name: string; | |
} | |
it("groupとalways両方してした場合。groupsが優先?", async () => { | |
const x = new X(); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class Name { | |
constructor(value: string) { | |
this.value = value; | |
} | |
@IsNotEmpty({ always: false }) | |
@MinLength(1, { always: false }) | |
value: string; | |
} | |
class X { | |
constructor(name: Name) { | |
this.name = name; | |
} | |
@IsNotEmpty({ always: true }) | |
@ValidateNested({ always: true }) | |
name: Name; | |
} | |
it("nestedする場合、nested内がalways=falseだと、下位の方が優先?", async () => { | |
const x = new X(new Name("")); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class Name { | |
constructor(value: string) { | |
this.value = value; | |
} | |
@IsNotEmpty({ always: true }) | |
@MinLength(1, { always: true }) | |
value: string; | |
} | |
class X { | |
constructor(name: Name) { | |
this.name = name; | |
} | |
@IsNotEmpty({ always: true }) | |
@ValidateNested({ always: true }) | |
name: Name; | |
} | |
it("そのため、nested内も含めてalwaysが必要", async () => { | |
const x = new X(new Name("")); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class Name { | |
constructor(value: string) { | |
this.value = value; | |
} | |
@IsNotEmpty({ always: true }) | |
@MinLength(1, { always: true }) | |
value: string; | |
} | |
class X { | |
constructor(name: Name) { | |
this.name = name; | |
} | |
@IsNotEmpty({ groups: ["c"] }) | |
@ValidateNested({ groups: ["c"] }) | |
name: Name; | |
} | |
it("nestedにgroup指定し、下位always=trueの場合。groupにマッチしたものだけ検証する", async () => { | |
const x = new X(new Name("")); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty; | |
}); | |
}); | |
describe("", () => { | |
class Name { | |
constructor(value: string) { | |
this.value = value; | |
} | |
@IsNotEmpty({ always: false }) | |
@MinLength(1, { always: false }) | |
value: string; | |
} | |
class X { | |
constructor(name: Name) { | |
this.name = name; | |
} | |
@IsNotEmpty({ groups: ["c"] }) | |
@ValidateNested({ groups: ["c"] }) | |
name: Name; | |
} | |
it("nestedにgroup指定し、下位always=falseの場合。groupにはどれもマッチしなくなる", async () => { | |
const x = new X(new Name("")); | |
expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty; | |
expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty; | |
expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty; | |
expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment