1 | import type {FuncKeywordDefinition, SchemaCxt} from "ajv"
|
---|
2 |
|
---|
3 | const sequences: Record<string, number | undefined> = {}
|
---|
4 |
|
---|
5 | export type DynamicDefaultFunc = (args?: Record<string, any>) => () => any
|
---|
6 |
|
---|
7 | const DEFAULTS: Record<string, DynamicDefaultFunc | undefined> = {
|
---|
8 | timestamp: () => () => Date.now(),
|
---|
9 | datetime: () => () => new Date().toISOString(),
|
---|
10 | date: () => () => new Date().toISOString().slice(0, 10),
|
---|
11 | time: () => () => new Date().toISOString().slice(11),
|
---|
12 | random: () => () => Math.random(),
|
---|
13 | randomint: (args?: {max?: number}) => {
|
---|
14 | const max = args?.max ?? 2
|
---|
15 | return () => Math.floor(Math.random() * max)
|
---|
16 | },
|
---|
17 | seq: (args?: {name?: string}) => {
|
---|
18 | const name = args?.name ?? ""
|
---|
19 | sequences[name] ||= 0
|
---|
20 | return () => (sequences[name] as number)++
|
---|
21 | },
|
---|
22 | }
|
---|
23 |
|
---|
24 | interface PropertyDefaultSchema {
|
---|
25 | func: string
|
---|
26 | args: Record<string, any>
|
---|
27 | }
|
---|
28 |
|
---|
29 | type DefaultSchema = Record<string, string | PropertyDefaultSchema | undefined>
|
---|
30 |
|
---|
31 | const getDef: (() => FuncKeywordDefinition) & {
|
---|
32 | DEFAULTS: typeof DEFAULTS
|
---|
33 | } = Object.assign(_getDef, {DEFAULTS})
|
---|
34 |
|
---|
35 | function _getDef(): FuncKeywordDefinition {
|
---|
36 | return {
|
---|
37 | keyword: "dynamicDefaults",
|
---|
38 | type: "object",
|
---|
39 | schemaType: ["string", "object"],
|
---|
40 | modifying: true,
|
---|
41 | valid: true,
|
---|
42 | compile(schema: DefaultSchema, _parentSchema, it: SchemaCxt) {
|
---|
43 | if (!it.opts.useDefaults || it.compositeRule) return () => true
|
---|
44 | const fs: Record<string, () => any> = {}
|
---|
45 | for (const key in schema) fs[key] = getDefault(schema[key])
|
---|
46 | const empty = it.opts.useDefaults === "empty"
|
---|
47 |
|
---|
48 | return (data: Record<string, any>) => {
|
---|
49 | for (const prop in schema) {
|
---|
50 | if (data[prop] === undefined || (empty && (data[prop] === null || data[prop] === ""))) {
|
---|
51 | data[prop] = fs[prop]()
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return true
|
---|
55 | }
|
---|
56 | },
|
---|
57 | metaSchema: {
|
---|
58 | type: "object",
|
---|
59 | additionalProperties: {
|
---|
60 | anyOf: [
|
---|
61 | {type: "string"},
|
---|
62 | {
|
---|
63 | type: "object",
|
---|
64 | additionalProperties: false,
|
---|
65 | required: ["func", "args"],
|
---|
66 | properties: {
|
---|
67 | func: {type: "string"},
|
---|
68 | args: {type: "object"},
|
---|
69 | },
|
---|
70 | },
|
---|
71 | ],
|
---|
72 | },
|
---|
73 | },
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | function getDefault(d: string | PropertyDefaultSchema | undefined): () => any {
|
---|
78 | return typeof d == "object" ? getObjDefault(d) : getStrDefault(d)
|
---|
79 | }
|
---|
80 |
|
---|
81 | function getObjDefault({func, args}: PropertyDefaultSchema): () => any {
|
---|
82 | const def = DEFAULTS[func]
|
---|
83 | assertDefined(func, def)
|
---|
84 | return def(args)
|
---|
85 | }
|
---|
86 |
|
---|
87 | function getStrDefault(d = ""): () => any {
|
---|
88 | const def = DEFAULTS[d]
|
---|
89 | assertDefined(d, def)
|
---|
90 | return def()
|
---|
91 | }
|
---|
92 |
|
---|
93 | function assertDefined(name: string, def?: DynamicDefaultFunc): asserts def is DynamicDefaultFunc {
|
---|
94 | if (!def) throw new Error(`invalid "dynamicDefaults" keyword property value: ${name}`)
|
---|
95 | }
|
---|
96 |
|
---|
97 | export default getDef
|
---|
98 | module.exports = getDef
|
---|