1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const codegen_1 = require("ajv/dist/compile/codegen");
|
---|
4 | const transform = {
|
---|
5 | trimStart: (s) => s.trimStart(),
|
---|
6 | trimEnd: (s) => s.trimEnd(),
|
---|
7 | trimLeft: (s) => s.trimStart(),
|
---|
8 | trimRight: (s) => s.trimEnd(),
|
---|
9 | trim: (s) => s.trim(),
|
---|
10 | toLowerCase: (s) => s.toLowerCase(),
|
---|
11 | toUpperCase: (s) => s.toUpperCase(),
|
---|
12 | toEnumCase: (s, cfg) => (cfg === null || cfg === void 0 ? void 0 : cfg.hash[configKey(s)]) || s,
|
---|
13 | };
|
---|
14 | const getDef = Object.assign(_getDef, { transform });
|
---|
15 | function _getDef() {
|
---|
16 | return {
|
---|
17 | keyword: "transform",
|
---|
18 | schemaType: "array",
|
---|
19 | before: "enum",
|
---|
20 | code(cxt) {
|
---|
21 | const { gen, data, schema, parentSchema, it } = cxt;
|
---|
22 | const { parentData, parentDataProperty } = it;
|
---|
23 | const tNames = schema;
|
---|
24 | if (!tNames.length)
|
---|
25 | return;
|
---|
26 | let cfg;
|
---|
27 | if (tNames.includes("toEnumCase")) {
|
---|
28 | const config = getEnumCaseCfg(parentSchema);
|
---|
29 | cfg = gen.scopeValue("obj", { ref: config, code: (0, codegen_1.stringify)(config) });
|
---|
30 | }
|
---|
31 | gen.if((0, codegen_1._) `typeof ${data} == "string" && ${parentData} !== undefined`, () => {
|
---|
32 | gen.assign(data, transformExpr(tNames.slice()));
|
---|
33 | gen.assign((0, codegen_1._) `${parentData}[${parentDataProperty}]`, data);
|
---|
34 | });
|
---|
35 | function transformExpr(ts) {
|
---|
36 | if (!ts.length)
|
---|
37 | return data;
|
---|
38 | const t = ts.pop();
|
---|
39 | if (!(t in transform))
|
---|
40 | throw new Error(`transform: unknown transformation ${t}`);
|
---|
41 | const func = gen.scopeValue("func", {
|
---|
42 | ref: transform[t],
|
---|
43 | code: (0, codegen_1._) `require("ajv-keywords/dist/definitions/transform").transform${(0, codegen_1.getProperty)(t)}`,
|
---|
44 | });
|
---|
45 | const arg = transformExpr(ts);
|
---|
46 | return cfg && t === "toEnumCase" ? (0, codegen_1._) `${func}(${arg}, ${cfg})` : (0, codegen_1._) `${func}(${arg})`;
|
---|
47 | }
|
---|
48 | },
|
---|
49 | metaSchema: {
|
---|
50 | type: "array",
|
---|
51 | items: { type: "string", enum: Object.keys(transform) },
|
---|
52 | },
|
---|
53 | };
|
---|
54 | }
|
---|
55 | function getEnumCaseCfg(parentSchema) {
|
---|
56 | // build hash table to enum values
|
---|
57 | const cfg = { hash: {} };
|
---|
58 | // requires `enum` in the same schema as transform
|
---|
59 | if (!parentSchema.enum)
|
---|
60 | throw new Error('transform: "toEnumCase" requires "enum"');
|
---|
61 | for (const v of parentSchema.enum) {
|
---|
62 | if (typeof v !== "string")
|
---|
63 | continue;
|
---|
64 | const k = configKey(v);
|
---|
65 | // requires all `enum` values have unique keys
|
---|
66 | if (cfg.hash[k]) {
|
---|
67 | throw new Error('transform: "toEnumCase" requires all lowercased "enum" values to be unique');
|
---|
68 | }
|
---|
69 | cfg.hash[k] = v;
|
---|
70 | }
|
---|
71 | return cfg;
|
---|
72 | }
|
---|
73 | function configKey(s) {
|
---|
74 | return s.toLowerCase();
|
---|
75 | }
|
---|
76 | exports.default = getDef;
|
---|
77 | module.exports = getDef;
|
---|
78 | //# sourceMappingURL=transform.js.map |
---|