-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (34 loc) · 1003 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Guard from "../../Guard";
import { ObjectSchema, SchemaType } from "./types";
import { SchemaGuard } from "./SchemaGuard";
export { ObjectSchema } from "./types";
/**
* It will validate that the given value is matching the object schema.
*
* `guard.name: JSON representation of the object`
*
* @param schema - Is a tree of guards. (Just a normal JS object, but with `Guard` values)
*
* @returns A `Guard`.
*
* @example
* ```ts
* const TUser = TObject({
* id: TInteger,
* name: TString,
* cart: {
* mangos: TInteger,
* avocados: TInteger,
* },
* });
*
* TUser.isValid({id: 1, name: "John" cart: {apples: 1}}) // false
* TUser.isValid({id: 1, name: "John" cart: {mangos: 1, avocados: 2}}) // true
* TUser.name === '{"id": "string(UUID)", "name": "string" cart: {"mangos": "integer", "avocados": "integeer"}}' // true
* ```
*/
export default function TObject<T extends ObjectSchema>(
schema: T
): Guard<SchemaType<T>> {
return new SchemaGuard(schema);
}