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