-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from thib3113/metas
allow to add metas in the schema
- Loading branch information
Showing
28 changed files
with
838 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
"use strict"; | ||
|
||
const Validator = require("../lib/validator"); | ||
const {RuleEmail} = require("../index"); | ||
|
||
describe("Test flat schema", () => { | ||
const v = new Validator(); | ||
|
@@ -1445,3 +1446,105 @@ describe("edge cases", () => { | |
]); | ||
}); | ||
}); | ||
|
||
describe("allow metas starting with $$", () => { | ||
const v = new Validator({ useNewCustomCheckerFunction: true }); | ||
describe("test on schema", () => { | ||
it("should not remove keys from source object", async () => { | ||
const schema = { | ||
$$foo: { | ||
foo: "bar" | ||
}, | ||
name: { type: "string" } }; | ||
const clonedSchema = {...schema}; | ||
v.compile(schema); | ||
|
||
expect(schema).toStrictEqual(clonedSchema); | ||
}); | ||
|
||
it("should works with $$root", () => { | ||
const schema = { | ||
$$foo: { | ||
foo: "bar" | ||
}, | ||
$$root: true, | ||
type: "email", | ||
empty: true | ||
}; | ||
const clonedSchema = {...schema}; | ||
const check = v.compile(schema); | ||
|
||
expect(check("[email protected]")).toEqual(true); | ||
expect(check("")).toEqual(true); | ||
expect(schema).toStrictEqual(clonedSchema); | ||
}); | ||
|
||
it("should works with $$async", async () => { | ||
const custom1 = jest.fn().mockResolvedValue("NAME"); | ||
const schema = { | ||
$$foo: { | ||
foo: "bar" | ||
}, | ||
$$async: true, | ||
name: { type: "string", custom: custom1 }, | ||
}; | ||
const clonedSchema = {...schema}; | ||
const check = v.compile(schema); | ||
|
||
//check schema meta was not changed | ||
expect(schema.$$foo).toStrictEqual(clonedSchema.$$foo); | ||
|
||
expect(check.async).toBe(true); | ||
|
||
let obj = { | ||
id: 3, | ||
name: "John", | ||
username: " john.doe ", | ||
age: 30 | ||
}; | ||
|
||
const res = await check(obj); | ||
expect(res).toBe(true); | ||
|
||
expect(custom1).toBeCalledTimes(1); | ||
expect(custom1).toBeCalledWith("John", [], schema.name, "name", null, expect.anything()); | ||
}); | ||
}); | ||
|
||
describe("test on rule", () => { | ||
it("should not remove keys from source object", async () => { | ||
const schema = { | ||
name: { | ||
$$foo: { | ||
foo: "bar" | ||
}, | ||
type: "string" | ||
} | ||
}; | ||
const clonedSchema = {...schema}; | ||
v.compile(schema); | ||
|
||
expect(schema).toStrictEqual(clonedSchema); | ||
}); | ||
it("should works with $$type", async () => { | ||
const schema = { | ||
dot: { | ||
$$foo: { | ||
foo: "bar" | ||
}, | ||
$$type: "object", | ||
x: "number", // object props here | ||
y: "number", // object props here | ||
} | ||
}; | ||
const clonedSchema = {...schema}; | ||
const check = v.compile(schema); | ||
|
||
expect(schema).toStrictEqual(clonedSchema); | ||
expect(check({ | ||
x: 1, | ||
y: 1, | ||
})).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.