source: imaps-frontend/node_modules/ajv-keywords/src/definitions/dynamicDefaults.ts

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.8 KB
Line 
1import type {FuncKeywordDefinition, SchemaCxt} from "ajv"
2
3const sequences: Record<string, number | undefined> = {}
4
5export type DynamicDefaultFunc = (args?: Record<string, any>) => () => any
6
7const 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
24interface PropertyDefaultSchema {
25 func: string
26 args: Record<string, any>
27}
28
29type DefaultSchema = Record<string, string | PropertyDefaultSchema | undefined>
30
31const getDef: (() => FuncKeywordDefinition) & {
32 DEFAULTS: typeof DEFAULTS
33} = Object.assign(_getDef, {DEFAULTS})
34
35function _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
77function getDefault(d: string | PropertyDefaultSchema | undefined): () => any {
78 return typeof d == "object" ? getObjDefault(d) : getStrDefault(d)
79}
80
81function getObjDefault({func, args}: PropertyDefaultSchema): () => any {
82 const def = DEFAULTS[func]
83 assertDefined(func, def)
84 return def(args)
85}
86
87function getStrDefault(d = ""): () => any {
88 const def = DEFAULTS[d]
89 assertDefined(d, def)
90 return def()
91}
92
93function assertDefined(name: string, def?: DynamicDefaultFunc): asserts def is DynamicDefaultFunc {
94 if (!def) throw new Error(`invalid "dynamicDefaults" keyword property value: ${name}`)
95}
96
97export default getDef
98module.exports = getDef
Note: See TracBrowser for help on using the repository browser.