@knapsack/schema-utils 4.70.0--canary.4821.ef34743.0 → 4.70.0--canary.4821.bd63208.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,95 +1,18 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
29
2
 
30
- // src/schema-utils.ts
31
- var schema_utils_exports = {};
32
- __export(schema_utils_exports, {
33
- ajv: () => ajv,
34
- validateDataAgainstSchema: () => validateDataAgainstSchema,
35
- validateSchema: () => validateSchema,
36
- validateSchemaAndAssignDefaults: () => validateSchemaAndAssignDefaults
37
- });
38
- module.exports = __toCommonJS(schema_utils_exports);
39
- var import_ajv = __toESM(require("ajv"));
40
- var import_typeof = __toESM(require("ajv-keywords/keywords/typeof.js"));
41
- var ajv = new import_ajv.default();
42
- (0, import_typeof.default)(ajv);
43
- var ajvDefaults = new import_ajv.default({
44
- useDefaults: true
45
- });
46
- function validateSchema(schema) {
47
- const isSchemaValid = ajv.validateSchema(schema);
48
- return {
49
- ok: isSchemaValid,
50
- message: isSchemaValid ? "" : `Schema itself is not valid. ${ajv.errorsText()}`
51
- };
52
- }
53
- __name(validateSchema, "validateSchema");
54
- function validateSchemaAndAssignDefaults(schema, data) {
55
- const { ok, message } = validateSchema(schema);
56
- if (!ok) {
57
- return {
58
- ok: false,
59
- message,
60
- data
61
- };
62
- }
63
- const newData = { ...data };
64
- const isValid = !!ajvDefaults.validate(schema, newData);
65
- return {
66
- ok: isValid,
67
- message: isValid ? "" : ajvDefaults.errorsText(),
68
- data: newData
69
- };
70
- }
71
- __name(validateSchemaAndAssignDefaults, "validateSchemaAndAssignDefaults");
72
- function validateDataAgainstSchema(schema, data) {
73
- const { ok, message } = validateSchema(schema);
74
- if (!ok) {
75
- return {
76
- ok: false,
77
- message
78
- };
79
- }
80
- const isValid = !!ajv.validate(schema, data);
81
- return {
82
- ok: isValid,
83
- message: isValid ? "" : ajv.errorsText(),
84
- errors: isValid ? [] : ajv.errors
85
- };
86
- }
87
- __name(validateDataAgainstSchema, "validateDataAgainstSchema");
88
- // Annotate the CommonJS export names for ESM import in node:
89
- 0 && (module.exports = {
90
- ajv,
91
- validateDataAgainstSchema,
92
- validateSchema,
93
- validateSchemaAndAssignDefaults
94
- });
3
+ var l = require('ajv');
4
+ var g = require('ajv-keywords/keywords/typeof.js');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var l__default = /*#__PURE__*/_interopDefault(l);
9
+ var g__default = /*#__PURE__*/_interopDefault(g);
10
+
11
+ var m=Object.defineProperty;var t=(e,r)=>m(e,"name",{value:r,configurable:!0});var s=new l__default.default;g__default.default(s);var c=new l__default.default({useDefaults:!0});function d(e){let r=s.validateSchema(e);return {ok:r,message:r?"":`Schema itself is not valid. ${s.errorsText()}`}}t(d,"validateSchema");function v(e,r){let{ok:a,message:n}=d(e);if(!a)return {ok:!1,message:n,data:r};let o={...r},i=!!c.validate(e,o);return {ok:i,message:i?"":c.errorsText(),data:o}}t(v,"validateSchemaAndAssignDefaults");function p(e,r){let{ok:a,message:n}=d(e);if(!a)return {ok:!1,message:n};let o=!!s.validate(e,r);return {ok:o,message:o?"":s.errorsText(),errors:o?[]:s.errors}}t(p,"validateDataAgainstSchema");
12
+
13
+ exports.ajv = s;
14
+ exports.validateDataAgainstSchema = p;
15
+ exports.validateSchema = d;
16
+ exports.validateSchemaAndAssignDefaults = v;
17
+ //# sourceMappingURL=schema-utils.js.map
95
18
  //# sourceMappingURL=schema-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/schema-utils.ts"],"sourcesContent":["import Ajv from 'ajv'; // https://github.com/epoberezkin/ajv\nimport addTypeofKeyword from 'ajv-keywords/keywords/typeof.js';\n\nexport const ajv = new Ajv();\n\naddTypeofKeyword(ajv);\n\ntype ajvErrors = import('ajv').ErrorObject[];\n\nconst ajvDefaults = new Ajv({\n useDefaults: true,\n});\n\n/**\n * Validate Schema itself (metaSchema)\n * Validates against contents of the `$schema` field\n * @link https://github.com/epoberezkin/ajv#validateschemaobject-schema---boolean\n */\nexport function validateSchema(schema: Record<string, unknown>): {\n ok: boolean;\n message: string;\n} {\n const isSchemaValid = ajv.validateSchema(schema);\n return {\n ok: isSchemaValid,\n message: isSchemaValid\n ? ''\n : `Schema itself is not valid. ${ajv.errorsText()}`,\n };\n}\n\n/**\n * Validate Schema and Assign Defaults\n * Any `default` on properties will be added if that property is absent\n * @link https://github.com/epoberezkin/ajv#assigning-defaults\n */\nexport function validateSchemaAndAssignDefaults<T>(\n schema: Record<string, unknown>,\n data: T,\n): { ok: boolean; message: string; data: T } {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n data,\n };\n }\n\n const newData = { ...data };\n // This `validate` function mutates `newData`, so that's why we created a new object above first.\n const isValid = !!ajvDefaults.validate(schema, newData);\n return {\n ok: isValid,\n message: isValid ? '' : ajvDefaults.errorsText(),\n data: newData,\n };\n}\n\n/**\n * Validate Data against Schema\n * @link https://github.com/epoberezkin/ajv\n */\nexport function validateDataAgainstSchema<T>(\n schema: Record<string, unknown>,\n data: T,\n): {\n ok: boolean;\n message: string;\n errors?: ajvErrors;\n} {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n };\n }\n\n const isValid = !!ajv.validate(schema, data);\n return {\n ok: isValid,\n message: isValid ? '' : ajv.errorsText(),\n errors: isValid ? [] : ajv.errors,\n };\n}\n\n//\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAgB;AAChB,oBAA6B;AAEtB,IAAM,MAAM,IAAI,WAAAA,QAAI;AAAA,IAE3B,cAAAC,SAAiB,GAAG;AAIpB,IAAM,cAAc,IAAI,WAAAD,QAAI;AAAA,EAC1B,aAAa;AACf,CAAC;AAOM,SAAS,eAAe,QAG7B;AACA,QAAM,gBAAgB,IAAI,eAAe,MAAM;AAC/C,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,gBACL,KACA,+BAA+B,IAAI,WAAW,CAAC;AAAA,EACrD;AACF;AAXgB;AAkBT,SAAS,gCACd,QACA,MAC2C;AAC3C,QAAM,EAAE,IAAI,QAAQ,IAAI,eAAe,MAAM;AAC7C,MAAI,CAAC,IAAI;AACP,WAAO;AAAA,MACL,IAAI;AAAA,MACJ;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,EAAE,GAAG,KAAK;AAE1B,QAAM,UAAU,CAAC,CAAC,YAAY,SAAS,QAAQ,OAAO;AACtD,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,UAAU,KAAK,YAAY,WAAW;AAAA,IAC/C,MAAM;AAAA,EACR;AACF;AArBgB;AA2BT,SAAS,0BACd,QACA,MAKA;AACA,QAAM,EAAE,IAAI,QAAQ,IAAI,eAAe,MAAM;AAC7C,MAAI,CAAC,IAAI;AACP,WAAO;AAAA,MACL,IAAI;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,CAAC,CAAC,IAAI,SAAS,QAAQ,IAAI;AAC3C,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,UAAU,KAAK,IAAI,WAAW;AAAA,IACvC,QAAQ,UAAU,CAAC,IAAI,IAAI;AAAA,EAC7B;AACF;AAtBgB;","names":["Ajv","addTypeofKeyword"]}
1
+ {"version":3,"sources":["../src/schema-utils.ts"],"names":["ajv","Ajv","addTypeofKeyword","ajvDefaults","validateSchema","schema","isSchemaValid","__name","validateSchemaAndAssignDefaults","data","ok","message","newData","isValid","validateDataAgainstSchema"],"mappings":";;;;;;;;;;AAAA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,CAAA,CAAA,CAAA,CAAA,KAGaA,CAAM,CAAA,IAAIC,mBAEvBC,kBAAAA,CAAiBF,CAAG,CAIpB,CAAA,IAAMG,CAAc,CAAA,IAAIF,mBAAI,CAC1B,WAAA,CAAa,CACf,CAAA,CAAC,EAOM,SAASG,CAAAA,CAAeC,CAG7B,CAAA,CACA,IAAMC,CAAgBN,CAAAA,CAAAA,CAAI,cAAeK,CAAAA,CAAM,EAC/C,OAAO,CACL,EAAIC,CAAAA,CAAAA,CACJ,QAASA,CACL,CAAA,EAAA,CACA,CAA+BN,4BAAAA,EAAAA,CAAAA,CAAI,YAAY,CAAA,CACrD,CACF,CAXgBO,EAAAH,CAAA,CAAA,gBAAA,CAAA,CAkBT,SAASI,CAAAA,CACdH,EACAI,CAC2C,CAAA,CAC3C,GAAM,CAAE,GAAAC,CAAI,CAAA,OAAA,CAAAC,CAAQ,CAAA,CAAIP,EAAeC,CAAM,CAAA,CAC7C,GAAI,CAACK,EACH,OAAO,CACL,EAAI,CAAA,CAAA,CAAA,CACJ,QAAAC,CACA,CAAA,IAAA,CAAAF,CACF,CAGF,CAAA,IAAMG,EAAU,CAAE,GAAGH,CAAK,CAAA,CAEpBI,EAAU,CAAC,CAACV,CAAY,CAAA,QAAA,CAASE,EAAQO,CAAO,CAAA,CACtD,OAAO,CACL,GAAIC,CACJ,CAAA,OAAA,CAASA,CAAU,CAAA,EAAA,CAAKV,EAAY,UAAW,EAAA,CAC/C,IAAMS,CAAAA,CACR,CACF,CArBgBL,CAAAA,CAAAC,CAAA,CAAA,iCAAA,CAAA,CA2BT,SAASM,CACdT,CAAAA,CAAAA,CACAI,CAKA,CAAA,CACA,GAAM,CAAE,EAAA,CAAAC,EAAI,OAAAC,CAAAA,CAAQ,EAAIP,CAAeC,CAAAA,CAAM,CAC7C,CAAA,GAAI,CAACK,CACH,CAAA,OAAO,CACL,EAAA,CAAI,GACJ,OAAAC,CAAAA,CACF,CAGF,CAAA,IAAME,EAAU,CAAC,CAACb,CAAI,CAAA,QAAA,CAASK,EAAQI,CAAI,CAAA,CAC3C,OAAO,CACL,GAAII,CACJ,CAAA,OAAA,CAASA,CAAU,CAAA,EAAA,CAAKb,EAAI,UAAW,EAAA,CACvC,MAAQa,CAAAA,CAAAA,CAAU,EAAKb,CAAAA,CAAAA,CAAI,MAC7B,CACF,CAtBgBO,EAAAO,CAAA,CAAA,2BAAA,CAAA","file":"schema-utils.js","sourcesContent":["import Ajv from 'ajv'; // https://github.com/epoberezkin/ajv\nimport addTypeofKeyword from 'ajv-keywords/keywords/typeof.js';\n\nexport const ajv = new Ajv();\n\naddTypeofKeyword(ajv);\n\ntype ajvErrors = import('ajv').ErrorObject[];\n\nconst ajvDefaults = new Ajv({\n useDefaults: true,\n});\n\n/**\n * Validate Schema itself (metaSchema)\n * Validates against contents of the `$schema` field\n * @link https://github.com/epoberezkin/ajv#validateschemaobject-schema---boolean\n */\nexport function validateSchema(schema: Record<string, unknown>): {\n ok: boolean;\n message: string;\n} {\n const isSchemaValid = ajv.validateSchema(schema);\n return {\n ok: isSchemaValid,\n message: isSchemaValid\n ? ''\n : `Schema itself is not valid. ${ajv.errorsText()}`,\n };\n}\n\n/**\n * Validate Schema and Assign Defaults\n * Any `default` on properties will be added if that property is absent\n * @link https://github.com/epoberezkin/ajv#assigning-defaults\n */\nexport function validateSchemaAndAssignDefaults<T>(\n schema: Record<string, unknown>,\n data: T,\n): { ok: boolean; message: string; data: T } {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n data,\n };\n }\n\n const newData = { ...data };\n // This `validate` function mutates `newData`, so that's why we created a new object above first.\n const isValid = !!ajvDefaults.validate(schema, newData);\n return {\n ok: isValid,\n message: isValid ? '' : ajvDefaults.errorsText(),\n data: newData,\n };\n}\n\n/**\n * Validate Data against Schema\n * @link https://github.com/epoberezkin/ajv\n */\nexport function validateDataAgainstSchema<T>(\n schema: Record<string, unknown>,\n data: T,\n): {\n ok: boolean;\n message: string;\n errors?: ajvErrors;\n} {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n };\n }\n\n const isValid = !!ajv.validate(schema, data);\n return {\n ok: isValid,\n message: isValid ? '' : ajv.errorsText(),\n errors: isValid ? [] : ajv.errors,\n };\n}\n\n//\n"]}
@@ -1,60 +1,8 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
1
+ import l from 'ajv';
2
+ import g from 'ajv-keywords/keywords/typeof.js';
3
3
 
4
- // src/schema-utils.ts
5
- import Ajv from "ajv";
6
- import addTypeofKeyword from "ajv-keywords/keywords/typeof.js";
7
- var ajv = new Ajv();
8
- addTypeofKeyword(ajv);
9
- var ajvDefaults = new Ajv({
10
- useDefaults: true
11
- });
12
- function validateSchema(schema) {
13
- const isSchemaValid = ajv.validateSchema(schema);
14
- return {
15
- ok: isSchemaValid,
16
- message: isSchemaValid ? "" : `Schema itself is not valid. ${ajv.errorsText()}`
17
- };
18
- }
19
- __name(validateSchema, "validateSchema");
20
- function validateSchemaAndAssignDefaults(schema, data) {
21
- const { ok, message } = validateSchema(schema);
22
- if (!ok) {
23
- return {
24
- ok: false,
25
- message,
26
- data
27
- };
28
- }
29
- const newData = { ...data };
30
- const isValid = !!ajvDefaults.validate(schema, newData);
31
- return {
32
- ok: isValid,
33
- message: isValid ? "" : ajvDefaults.errorsText(),
34
- data: newData
35
- };
36
- }
37
- __name(validateSchemaAndAssignDefaults, "validateSchemaAndAssignDefaults");
38
- function validateDataAgainstSchema(schema, data) {
39
- const { ok, message } = validateSchema(schema);
40
- if (!ok) {
41
- return {
42
- ok: false,
43
- message
44
- };
45
- }
46
- const isValid = !!ajv.validate(schema, data);
47
- return {
48
- ok: isValid,
49
- message: isValid ? "" : ajv.errorsText(),
50
- errors: isValid ? [] : ajv.errors
51
- };
52
- }
53
- __name(validateDataAgainstSchema, "validateDataAgainstSchema");
54
- export {
55
- ajv,
56
- validateDataAgainstSchema,
57
- validateSchema,
58
- validateSchemaAndAssignDefaults
59
- };
4
+ var m=Object.defineProperty;var t=(e,r)=>m(e,"name",{value:r,configurable:!0});var s=new l;g(s);var c=new l({useDefaults:!0});function d(e){let r=s.validateSchema(e);return {ok:r,message:r?"":`Schema itself is not valid. ${s.errorsText()}`}}t(d,"validateSchema");function v(e,r){let{ok:a,message:n}=d(e);if(!a)return {ok:!1,message:n,data:r};let o={...r},i=!!c.validate(e,o);return {ok:i,message:i?"":c.errorsText(),data:o}}t(v,"validateSchemaAndAssignDefaults");function p(e,r){let{ok:a,message:n}=d(e);if(!a)return {ok:!1,message:n};let o=!!s.validate(e,r);return {ok:o,message:o?"":s.errorsText(),errors:o?[]:s.errors}}t(p,"validateDataAgainstSchema");
5
+
6
+ export { s as ajv, p as validateDataAgainstSchema, d as validateSchema, v as validateSchemaAndAssignDefaults };
7
+ //# sourceMappingURL=schema-utils.mjs.map
60
8
  //# sourceMappingURL=schema-utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/schema-utils.ts"],"sourcesContent":["import Ajv from 'ajv'; // https://github.com/epoberezkin/ajv\nimport addTypeofKeyword from 'ajv-keywords/keywords/typeof.js';\n\nexport const ajv = new Ajv();\n\naddTypeofKeyword(ajv);\n\ntype ajvErrors = import('ajv').ErrorObject[];\n\nconst ajvDefaults = new Ajv({\n useDefaults: true,\n});\n\n/**\n * Validate Schema itself (metaSchema)\n * Validates against contents of the `$schema` field\n * @link https://github.com/epoberezkin/ajv#validateschemaobject-schema---boolean\n */\nexport function validateSchema(schema: Record<string, unknown>): {\n ok: boolean;\n message: string;\n} {\n const isSchemaValid = ajv.validateSchema(schema);\n return {\n ok: isSchemaValid,\n message: isSchemaValid\n ? ''\n : `Schema itself is not valid. ${ajv.errorsText()}`,\n };\n}\n\n/**\n * Validate Schema and Assign Defaults\n * Any `default` on properties will be added if that property is absent\n * @link https://github.com/epoberezkin/ajv#assigning-defaults\n */\nexport function validateSchemaAndAssignDefaults<T>(\n schema: Record<string, unknown>,\n data: T,\n): { ok: boolean; message: string; data: T } {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n data,\n };\n }\n\n const newData = { ...data };\n // This `validate` function mutates `newData`, so that's why we created a new object above first.\n const isValid = !!ajvDefaults.validate(schema, newData);\n return {\n ok: isValid,\n message: isValid ? '' : ajvDefaults.errorsText(),\n data: newData,\n };\n}\n\n/**\n * Validate Data against Schema\n * @link https://github.com/epoberezkin/ajv\n */\nexport function validateDataAgainstSchema<T>(\n schema: Record<string, unknown>,\n data: T,\n): {\n ok: boolean;\n message: string;\n errors?: ajvErrors;\n} {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n };\n }\n\n const isValid = !!ajv.validate(schema, data);\n return {\n ok: isValid,\n message: isValid ? '' : ajv.errorsText(),\n errors: isValid ? [] : ajv.errors,\n };\n}\n\n//\n"],"mappings":";;;;AAAA,OAAO,SAAS;AAChB,OAAO,sBAAsB;AAEtB,IAAM,MAAM,IAAI,IAAI;AAE3B,iBAAiB,GAAG;AAIpB,IAAM,cAAc,IAAI,IAAI;AAAA,EAC1B,aAAa;AACf,CAAC;AAOM,SAAS,eAAe,QAG7B;AACA,QAAM,gBAAgB,IAAI,eAAe,MAAM;AAC/C,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,gBACL,KACA,+BAA+B,IAAI,WAAW,CAAC;AAAA,EACrD;AACF;AAXgB;AAkBT,SAAS,gCACd,QACA,MAC2C;AAC3C,QAAM,EAAE,IAAI,QAAQ,IAAI,eAAe,MAAM;AAC7C,MAAI,CAAC,IAAI;AACP,WAAO;AAAA,MACL,IAAI;AAAA,MACJ;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,EAAE,GAAG,KAAK;AAE1B,QAAM,UAAU,CAAC,CAAC,YAAY,SAAS,QAAQ,OAAO;AACtD,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,UAAU,KAAK,YAAY,WAAW;AAAA,IAC/C,MAAM;AAAA,EACR;AACF;AArBgB;AA2BT,SAAS,0BACd,QACA,MAKA;AACA,QAAM,EAAE,IAAI,QAAQ,IAAI,eAAe,MAAM;AAC7C,MAAI,CAAC,IAAI;AACP,WAAO;AAAA,MACL,IAAI;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,CAAC,CAAC,IAAI,SAAS,QAAQ,IAAI;AAC3C,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,SAAS,UAAU,KAAK,IAAI,WAAW;AAAA,IACvC,QAAQ,UAAU,CAAC,IAAI,IAAI;AAAA,EAC7B;AACF;AAtBgB;","names":[]}
1
+ {"version":3,"sources":["../src/schema-utils.ts"],"names":["ajv","Ajv","addTypeofKeyword","ajvDefaults","validateSchema","schema","isSchemaValid","__name","validateSchemaAndAssignDefaults","data","ok","message","newData","isValid","validateDataAgainstSchema"],"mappings":";;;AAAA,IAAA,CAAA,CAAA,MAAA,CAAA,cAAA,CAAA,IAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,GAAA,CAAA,CAAA,CAAA,CAAA,MAAA,CAAA,CAAA,KAAA,CAAA,CAAA,CAAA,YAAA,CAAA,CAAA,CAAA,CAAA,CAAA,KAGaA,CAAM,CAAA,IAAIC,EAEvBC,CAAAA,CAAiBF,CAAG,CAIpB,CAAA,IAAMG,CAAc,CAAA,IAAIF,EAAI,CAC1B,WAAA,CAAa,CACf,CAAA,CAAC,EAOM,SAASG,CAAAA,CAAeC,CAG7B,CAAA,CACA,IAAMC,CAAgBN,CAAAA,CAAAA,CAAI,cAAeK,CAAAA,CAAM,EAC/C,OAAO,CACL,EAAIC,CAAAA,CAAAA,CACJ,QAASA,CACL,CAAA,EAAA,CACA,CAA+BN,4BAAAA,EAAAA,CAAAA,CAAI,YAAY,CAAA,CACrD,CACF,CAXgBO,EAAAH,CAAA,CAAA,gBAAA,CAAA,CAkBT,SAASI,CAAAA,CACdH,EACAI,CAC2C,CAAA,CAC3C,GAAM,CAAE,GAAAC,CAAI,CAAA,OAAA,CAAAC,CAAQ,CAAA,CAAIP,EAAeC,CAAM,CAAA,CAC7C,GAAI,CAACK,EACH,OAAO,CACL,EAAI,CAAA,CAAA,CAAA,CACJ,QAAAC,CACA,CAAA,IAAA,CAAAF,CACF,CAGF,CAAA,IAAMG,EAAU,CAAE,GAAGH,CAAK,CAAA,CAEpBI,EAAU,CAAC,CAACV,CAAY,CAAA,QAAA,CAASE,EAAQO,CAAO,CAAA,CACtD,OAAO,CACL,GAAIC,CACJ,CAAA,OAAA,CAASA,CAAU,CAAA,EAAA,CAAKV,EAAY,UAAW,EAAA,CAC/C,IAAMS,CAAAA,CACR,CACF,CArBgBL,CAAAA,CAAAC,CAAA,CAAA,iCAAA,CAAA,CA2BT,SAASM,CACdT,CAAAA,CAAAA,CACAI,CAKA,CAAA,CACA,GAAM,CAAE,EAAA,CAAAC,EAAI,OAAAC,CAAAA,CAAQ,EAAIP,CAAeC,CAAAA,CAAM,CAC7C,CAAA,GAAI,CAACK,CACH,CAAA,OAAO,CACL,EAAA,CAAI,GACJ,OAAAC,CAAAA,CACF,CAGF,CAAA,IAAME,EAAU,CAAC,CAACb,CAAI,CAAA,QAAA,CAASK,EAAQI,CAAI,CAAA,CAC3C,OAAO,CACL,GAAII,CACJ,CAAA,OAAA,CAASA,CAAU,CAAA,EAAA,CAAKb,EAAI,UAAW,EAAA,CACvC,MAAQa,CAAAA,CAAAA,CAAU,EAAKb,CAAAA,CAAAA,CAAI,MAC7B,CACF,CAtBgBO,EAAAO,CAAA,CAAA,2BAAA,CAAA","file":"schema-utils.mjs","sourcesContent":["import Ajv from 'ajv'; // https://github.com/epoberezkin/ajv\nimport addTypeofKeyword from 'ajv-keywords/keywords/typeof.js';\n\nexport const ajv = new Ajv();\n\naddTypeofKeyword(ajv);\n\ntype ajvErrors = import('ajv').ErrorObject[];\n\nconst ajvDefaults = new Ajv({\n useDefaults: true,\n});\n\n/**\n * Validate Schema itself (metaSchema)\n * Validates against contents of the `$schema` field\n * @link https://github.com/epoberezkin/ajv#validateschemaobject-schema---boolean\n */\nexport function validateSchema(schema: Record<string, unknown>): {\n ok: boolean;\n message: string;\n} {\n const isSchemaValid = ajv.validateSchema(schema);\n return {\n ok: isSchemaValid,\n message: isSchemaValid\n ? ''\n : `Schema itself is not valid. ${ajv.errorsText()}`,\n };\n}\n\n/**\n * Validate Schema and Assign Defaults\n * Any `default` on properties will be added if that property is absent\n * @link https://github.com/epoberezkin/ajv#assigning-defaults\n */\nexport function validateSchemaAndAssignDefaults<T>(\n schema: Record<string, unknown>,\n data: T,\n): { ok: boolean; message: string; data: T } {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n data,\n };\n }\n\n const newData = { ...data };\n // This `validate` function mutates `newData`, so that's why we created a new object above first.\n const isValid = !!ajvDefaults.validate(schema, newData);\n return {\n ok: isValid,\n message: isValid ? '' : ajvDefaults.errorsText(),\n data: newData,\n };\n}\n\n/**\n * Validate Data against Schema\n * @link https://github.com/epoberezkin/ajv\n */\nexport function validateDataAgainstSchema<T>(\n schema: Record<string, unknown>,\n data: T,\n): {\n ok: boolean;\n message: string;\n errors?: ajvErrors;\n} {\n const { ok, message } = validateSchema(schema);\n if (!ok) {\n return {\n ok: false,\n message,\n };\n }\n\n const isValid = !!ajv.validate(schema, data);\n return {\n ok: isValid,\n message: isValid ? '' : ajv.errorsText(),\n errors: isValid ? [] : ajv.errors,\n };\n}\n\n//\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@knapsack/schema-utils",
3
3
  "description": "A Knapsack component that helps build Design Systems",
4
- "version": "4.70.0--canary.4821.ef34743.0",
4
+ "version": "4.70.0--canary.4821.bd63208.0",
5
5
  "source": "src/schema-utils.ts",
6
6
  "main": "dist/schema-utils.js",
7
7
  "module": "dist/schema-utils.mjs",
@@ -29,8 +29,8 @@
29
29
  "tslib": "^2.7.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@knapsack/eslint-config-starter": "4.70.0--canary.4821.ef34743.0",
33
- "@knapsack/typescript-config-starter": "4.70.0--canary.4821.ef34743.0",
32
+ "@knapsack/eslint-config-starter": "4.70.0--canary.4821.bd63208.0",
33
+ "@knapsack/typescript-config-starter": "4.70.0--canary.4821.bd63208.0",
34
34
  "eslint": "^8.57.0",
35
35
  "tsup": "^8.2.4"
36
36
  },
@@ -43,5 +43,5 @@
43
43
  "directory": "libs/schema-utils",
44
44
  "type": "git"
45
45
  },
46
- "gitHead": "ef34743371e8e5d6c05f0f3dac6cd756c9ebb078"
46
+ "gitHead": "bd6320889f9e7ea11ea57943e22d8da2493ff786"
47
47
  }