Advertisement
Guest User

Untitled

a guest
May 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import "es6-shim";
  2. import {IsNotEmpty, ValidateIf, IsOptional, Equals} from "../src/decorator/decorators";
  3. import {Validator} from "../src/validation/Validator";
  4. import {ValidatorOptions} from "../src/validation/ValidatorOptions";
  5. import {expect, should, use } from "chai";
  6.  
  7. import * as chaiAsPromised from "chai-as-promised";
  8. import { MinLength, ValidateNested } from "../src";
  9.  
  10. should();
  11. use(chaiAsPromised);
  12.  
  13. // -------------------------------------------------------------------------
  14. // Setup
  15. // -------------------------------------------------------------------------
  16.  
  17. const validator = new Validator();
  18.  
  19. describe.only("labo", () => {
  20. describe("", () => {
  21. class X {
  22. @IsNotEmpty({ always: true })
  23. name: string;
  24. }
  25. it("alwaysの場合、グループ指定してもvalidation実行する", async () => {
  26. const x = new X();
  27. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  28. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  29. expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty;
  30. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  31. });
  32. });
  33.  
  34. describe("", () => {
  35. class X {
  36. @IsNotEmpty({ always: false })
  37. name: string;
  38. }
  39. it("always=falseの場合、グループにマッチで実行する", async () => {
  40. const x = new X();
  41. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  42. expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty;
  43. expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty;
  44. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty;
  45. });
  46. });
  47.  
  48. describe("", () => {
  49. class X {
  50. @IsNotEmpty({ groups: ["c"] })
  51. name: string;
  52. }
  53. it("group指定した場合、いずれかにマッチで実行する", async () => {
  54. const x = new X();
  55. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  56. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  57. expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty;
  58. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  59. });
  60. });
  61.  
  62. describe("", () => {
  63. class X {
  64. @IsNotEmpty({ groups: ["c", "d"] })
  65. name: string;
  66. }
  67. it("group指定した場合、いずれかにマッチで実行する。複数でも同様", async () => {
  68. const x = new X();
  69. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  70. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  71. expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty;
  72. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  73. });
  74. });
  75.  
  76. describe("", () => {
  77. class X {
  78. @IsNotEmpty({ groups: ["c", "d"], always: true })
  79. name: string;
  80. }
  81. it("groupとalways両方してした場合。groupsが優先?", async () => {
  82. const x = new X();
  83. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  84. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  85. expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty;
  86. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  87. });
  88. });
  89.  
  90. describe("", () => {
  91. class Name {
  92. constructor(value: string) {
  93. this.value = value;
  94. }
  95. @IsNotEmpty({ always: false })
  96. @MinLength(1, { always: false })
  97. value: string;
  98. }
  99. class X {
  100. constructor(name: Name) {
  101. this.name = name;
  102. }
  103. @IsNotEmpty({ always: true })
  104. @ValidateNested({ always: true })
  105. name: Name;
  106. }
  107.  
  108. it("nestedする場合、nested内がalways=falseだと、下位の方が優先?", async () => {
  109.  
  110. const x = new X(new Name(""));
  111. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  112. expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty;
  113. expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty;
  114. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty;
  115.  
  116. });
  117. });
  118. describe("", () => {
  119. class Name {
  120. constructor(value: string) {
  121. this.value = value;
  122. }
  123. @IsNotEmpty({ always: true })
  124. @MinLength(1, { always: true })
  125. value: string;
  126. }
  127. class X {
  128. constructor(name: Name) {
  129. this.name = name;
  130. }
  131. @IsNotEmpty({ always: true })
  132. @ValidateNested({ always: true })
  133. name: Name;
  134. }
  135.  
  136. it("そのため、nested内も含めてalwaysが必要", async () => {
  137.  
  138. const x = new X(new Name(""));
  139. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  140. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  141. expect(await validator.validate(x, { groups: ["d"]}), "case3").not.to.be.empty;
  142. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  143.  
  144. });
  145. });
  146.  
  147. describe("", () => {
  148. class Name {
  149. constructor(value: string) {
  150. this.value = value;
  151. }
  152. @IsNotEmpty({ always: true })
  153. @MinLength(1, { always: true })
  154. value: string;
  155. }
  156. class X {
  157. constructor(name: Name) {
  158. this.name = name;
  159. }
  160. @IsNotEmpty({ groups: ["c"] })
  161. @ValidateNested({ groups: ["c"] })
  162. name: Name;
  163. }
  164.  
  165. it("nestedにgroup指定し、下位always=trueの場合。groupにマッチしたものだけ検証する", async () => {
  166.  
  167. const x = new X(new Name(""));
  168. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  169. expect(await validator.validate(x, { groups: ["c"]}), "case2").not.to.be.empty;
  170. expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty;
  171. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").not.to.be.empty;
  172.  
  173. });
  174. });
  175.  
  176. describe("", () => {
  177. class Name {
  178. constructor(value: string) {
  179. this.value = value;
  180. }
  181. @IsNotEmpty({ always: false })
  182. @MinLength(1, { always: false })
  183. value: string;
  184. }
  185. class X {
  186. constructor(name: Name) {
  187. this.name = name;
  188. }
  189. @IsNotEmpty({ groups: ["c"] })
  190. @ValidateNested({ groups: ["c"] })
  191. name: Name;
  192. }
  193.  
  194. it("nestedにgroup指定し、下位always=falseの場合。groupにはどれもマッチしなくなる", async () => {
  195.  
  196. const x = new X(new Name(""));
  197. expect(await validator.validate(x, { groups: []}), "case1").not.to.be.empty;
  198. expect(await validator.validate(x, { groups: ["c"]}), "case2").to.be.empty;
  199. expect(await validator.validate(x, { groups: ["d"]}), "case3").to.be.empty;
  200. expect(await validator.validate(x, { groups: ["c", "d"]}), "case4").to.be.empty;
  201.  
  202. });
  203. });
  204.  
  205. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement