@stndrds/schema 0.1.0-alpha.67 → 1.0.0-alpha.102
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/attributes-D7yQuTJK.d.mts +665 -0
- package/dist/attributes-DcbQC-4b.d.ts +665 -0
- package/dist/chunk-3JINFUN3.js +81 -0
- package/dist/chunk-4KRLCWJM.mjs +8 -0
- package/dist/chunk-4TC27SMF.mjs +18 -0
- package/dist/chunk-56QI6UTK.js +32 -0
- package/dist/chunk-5JVVPJJW.mjs +208 -0
- package/dist/chunk-5XL3JP73.mjs +83 -0
- package/dist/chunk-7HFZ7MVR.mjs +28 -0
- package/dist/chunk-ACQ3TUFK.js +10 -0
- package/dist/chunk-AYCSVKI2.mjs +14 -0
- package/dist/chunk-BLXQGQLK.mjs +10 -0
- package/dist/chunk-D7WFIGXW.js +14 -0
- package/dist/chunk-FRCDMQER.js +47 -0
- package/dist/chunk-GCRHSPZN.js +16 -0
- package/dist/chunk-IRTRJH37.mjs +8 -0
- package/dist/chunk-ITTQ4FGR.mjs +14 -0
- package/dist/chunk-J5HRK3WD.js +10 -0
- package/dist/chunk-KNYZH2WD.mjs +18 -0
- package/dist/chunk-KWVCGQ7N.mjs +78 -0
- package/dist/chunk-MAKSIK3P.js +10 -0
- package/dist/chunk-N7T66JVY.js +86 -0
- package/dist/chunk-OAQWRMTP.js +10 -0
- package/dist/chunk-OLJLCVNY.mjs +8 -0
- package/dist/chunk-P2BE2A2G.js +64 -0
- package/dist/chunk-PGERPYDR.js +2 -0
- package/dist/chunk-QWZHR2OO.js +12 -0
- package/dist/chunk-QY6QFRRV.mjs +41 -0
- package/dist/chunk-QYNPB37W.js +221 -0
- package/dist/chunk-R4CJHCC4.mjs +60 -0
- package/dist/chunk-RJ4LOX7P.js +22 -0
- package/dist/chunk-ROAZLZA2.js +20 -0
- package/dist/chunk-RUYUFXNW.mjs +12 -0
- package/dist/chunk-SP3PNHYF.mjs +1 -0
- package/dist/chunk-SS2NR6DH.mjs +176 -0
- package/dist/chunk-T225DB55.js +33 -0
- package/dist/chunk-TKN73433.js +20 -0
- package/dist/chunk-TMYBEXBT.mjs +8 -0
- package/dist/chunk-UQVW4KPP.mjs +30 -0
- package/dist/chunk-V6DE3MXC.js +180 -0
- package/dist/chunk-VXOTYFEW.js +16 -0
- package/dist/chunk-WV4V64D5.mjs +20 -0
- package/dist/helpers-DD6JzYfI.d.mts +860 -0
- package/dist/helpers-kMS5R7KX.d.ts +860 -0
- package/dist/index.d.mts +3597 -1768
- package/dist/index.d.ts +3597 -1768
- package/dist/index.js +4957 -1955
- package/dist/index.mjs +3920 -1116
- package/dist/types-BAPgWlpX.d.mts +39 -0
- package/dist/types-mZPUEZl0.d.ts +39 -0
- package/dist/utils.d.mts +20 -55
- package/dist/utils.d.ts +20 -55
- package/dist/utils.js +26 -19
- package/dist/utils.mjs +1 -19
- package/dist/validation/all.d.mts +8 -0
- package/dist/validation/all.d.ts +8 -0
- package/dist/validation/all.js +56 -0
- package/dist/validation/all.mjs +19 -0
- package/dist/validation/complex/currency.d.mts +15 -0
- package/dist/validation/complex/currency.d.ts +15 -0
- package/dist/validation/complex/currency.js +11 -0
- package/dist/validation/complex/currency.mjs +2 -0
- package/dist/validation/complex/file.d.mts +13 -0
- package/dist/validation/complex/file.d.ts +13 -0
- package/dist/validation/complex/file.js +11 -0
- package/dist/validation/complex/file.mjs +2 -0
- package/dist/validation/complex/location.d.mts +22 -0
- package/dist/validation/complex/location.d.ts +22 -0
- package/dist/validation/complex/location.js +11 -0
- package/dist/validation/complex/location.mjs +2 -0
- package/dist/validation/complex/phone.d.mts +22 -0
- package/dist/validation/complex/phone.d.ts +22 -0
- package/dist/validation/complex/phone.js +11 -0
- package/dist/validation/complex/phone.mjs +2 -0
- package/dist/validation/complex/relation.d.mts +25 -0
- package/dist/validation/complex/relation.d.ts +25 -0
- package/dist/validation/complex/relation.js +19 -0
- package/dist/validation/complex/relation.mjs +2 -0
- package/dist/validation/complex/richtext.d.mts +13 -0
- package/dist/validation/complex/richtext.d.ts +13 -0
- package/dist/validation/complex/richtext.js +11 -0
- package/dist/validation/complex/richtext.mjs +2 -0
- package/dist/validation/complex/select.d.mts +20 -0
- package/dist/validation/complex/select.d.ts +20 -0
- package/dist/validation/complex/select.js +19 -0
- package/dist/validation/complex/select.mjs +2 -0
- package/dist/validation/complex/user.d.mts +13 -0
- package/dist/validation/complex/user.d.ts +13 -0
- package/dist/validation/complex/user.js +11 -0
- package/dist/validation/complex/user.mjs +2 -0
- package/dist/validation/computed/formula.d.mts +14 -0
- package/dist/validation/computed/formula.d.ts +14 -0
- package/dist/validation/computed/formula.js +11 -0
- package/dist/validation/computed/formula.mjs +2 -0
- package/dist/validation/computed/rollup.d.mts +14 -0
- package/dist/validation/computed/rollup.d.ts +14 -0
- package/dist/validation/computed/rollup.js +11 -0
- package/dist/validation/computed/rollup.mjs +2 -0
- package/dist/validation/config/index.d.mts +20 -0
- package/dist/validation/config/index.d.ts +20 -0
- package/dist/validation/config/index.js +18 -0
- package/dist/validation/config/index.mjs +1 -0
- package/dist/validation/core/index.d.mts +19 -0
- package/dist/validation/core/index.d.ts +19 -0
- package/dist/validation/core/index.js +14 -0
- package/dist/validation/core/index.mjs +1 -0
- package/dist/validation/object/index.d.mts +31 -0
- package/dist/validation/object/index.d.ts +31 -0
- package/dist/validation/object/index.js +70 -0
- package/dist/validation/object/index.mjs +17 -0
- package/dist/validation/primitives/checkbox.d.mts +12 -0
- package/dist/validation/primitives/checkbox.d.ts +12 -0
- package/dist/validation/primitives/checkbox.js +11 -0
- package/dist/validation/primitives/checkbox.mjs +2 -0
- package/dist/validation/primitives/date.d.mts +12 -0
- package/dist/validation/primitives/date.d.ts +12 -0
- package/dist/validation/primitives/date.js +11 -0
- package/dist/validation/primitives/date.mjs +2 -0
- package/dist/validation/primitives/number.d.mts +12 -0
- package/dist/validation/primitives/number.d.ts +12 -0
- package/dist/validation/primitives/number.js +11 -0
- package/dist/validation/primitives/number.mjs +2 -0
- package/dist/validation/primitives/rating.d.mts +12 -0
- package/dist/validation/primitives/rating.d.ts +12 -0
- package/dist/validation/primitives/rating.js +11 -0
- package/dist/validation/primitives/rating.mjs +2 -0
- package/dist/validation/primitives/text.d.mts +17 -0
- package/dist/validation/primitives/text.d.ts +17 -0
- package/dist/validation/primitives/text.js +15 -0
- package/dist/validation/primitives/text.mjs +2 -0
- package/package.json +108 -13
- package/dist/chunk-36UBIXJN.js +0 -82
- package/dist/chunk-3RG5ZIWI.js +0 -10
- package/dist/chunk-A6T4UUZ2.mjs +0 -18669
- package/dist/chunk-NEVERCM3.js +0 -41
- package/dist/chunk-SV4BCGQU.mjs +0 -617
- package/dist/chunk-TOTUS3YS.js +0 -18669
- package/dist/chunk-U4AB53AM.js +0 -617
- package/dist/chunk-V2RPPE2Y.mjs +0 -41
- package/dist/chunk-V5QXU2OK.mjs +0 -82
- package/dist/chunk-Y6FXYEAI.mjs +0 -10
- package/dist/default-roles-42X3TJI5.mjs +0 -21
- package/dist/default-roles-C3FYDYMN.js +0 -21
- package/dist/runtime-BgtwoMx5.d.ts +0 -12759
- package/dist/runtime-DQlTgVZJ.d.mts +0 -12759
- package/dist/runtime.d.mts +0 -6
- package/dist/runtime.d.ts +0 -6
- package/dist/runtime.js +0 -323
- package/dist/runtime.mjs +0 -323
- package/dist/validation/validators.d.mts +0 -4
- package/dist/validation/validators.d.ts +0 -4
- package/dist/validation/validators.js +0 -121
- package/dist/validation/validators.mjs +0 -121
- package/dist/validators-BROsLGn4.d.mts +0 -1556
- package/dist/validators-CEzdvxEq.d.ts +0 -1556
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
// src/lib/lru-cache.ts
|
|
7
|
+
var LRUCache = class {
|
|
8
|
+
constructor(capacity) {
|
|
9
|
+
if (capacity <= 0) {
|
|
10
|
+
throw new Error("LRU cache capacity must be greater than 0");
|
|
11
|
+
}
|
|
12
|
+
this.capacity = capacity;
|
|
13
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
14
|
+
}
|
|
15
|
+
get(key) {
|
|
16
|
+
const value = this.cache.get(key);
|
|
17
|
+
if (value !== void 0) {
|
|
18
|
+
this.cache.delete(key);
|
|
19
|
+
this.cache.set(key, value);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
set(key, value) {
|
|
24
|
+
if (this.cache.has(key)) {
|
|
25
|
+
this.cache.delete(key);
|
|
26
|
+
} else if (this.cache.size >= this.capacity) {
|
|
27
|
+
const firstKey = this.cache.keys().next().value;
|
|
28
|
+
if (firstKey !== void 0) {
|
|
29
|
+
this.cache.delete(firstKey);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
this.cache.set(key, value);
|
|
33
|
+
}
|
|
34
|
+
has(key) {
|
|
35
|
+
return this.cache.has(key);
|
|
36
|
+
}
|
|
37
|
+
clear() {
|
|
38
|
+
this.cache.clear();
|
|
39
|
+
}
|
|
40
|
+
get size() {
|
|
41
|
+
return this.cache.size;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/validation/core/regex-cache.ts
|
|
46
|
+
function createRegexCache(capacity = 100) {
|
|
47
|
+
return new LRUCache(capacity);
|
|
48
|
+
}
|
|
49
|
+
function getCachedRegex(cache, pattern) {
|
|
50
|
+
let cached = cache.get(pattern);
|
|
51
|
+
if (!cached) {
|
|
52
|
+
cached = new RegExp(pattern);
|
|
53
|
+
cache.set(pattern, cached);
|
|
54
|
+
}
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/validation/primitives/text.ts
|
|
59
|
+
var textRegexCache = createRegexCache();
|
|
60
|
+
function createTextValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
61
|
+
let schema = zod.z.string();
|
|
62
|
+
if (attr.minLength !== void 0) {
|
|
63
|
+
schema = schema.min(attr.minLength, messages.minLength(attr, attr.minLength));
|
|
64
|
+
}
|
|
65
|
+
if (attr.maxLength !== void 0) {
|
|
66
|
+
schema = schema.max(attr.maxLength, messages.maxLength(attr, attr.maxLength));
|
|
67
|
+
}
|
|
68
|
+
if (attr.pattern) {
|
|
69
|
+
schema = schema.regex(
|
|
70
|
+
getCachedRegex(textRegexCache, attr.pattern),
|
|
71
|
+
messages.invalidPattern(attr)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return schema;
|
|
75
|
+
}
|
|
76
|
+
function createTextAreaValidator(_attr, _messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
77
|
+
return zod.z.string();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
exports.createTextAreaValidator = createTextAreaValidator;
|
|
81
|
+
exports.createTextValidator = createTextValidator;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createDateValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
return z.coerce.date({ message: messages.invalidDate(attr) });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export { createDateValidator };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createFileValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
const uuidSchema = z.uuid({
|
|
6
|
+
message: messages.invalidId(attr)
|
|
7
|
+
});
|
|
8
|
+
if (attr.multiple) {
|
|
9
|
+
let arraySchema = z.array(uuidSchema);
|
|
10
|
+
if (attr.maxFiles) {
|
|
11
|
+
arraySchema = arraySchema.max(attr.maxFiles, messages.maxItems(attr, attr.maxFiles));
|
|
12
|
+
}
|
|
13
|
+
return arraySchema;
|
|
14
|
+
}
|
|
15
|
+
return uuidSchema;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { createFileValidator };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function createSelectValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
7
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
8
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
9
|
+
return zod.z.enum(validValues, {
|
|
10
|
+
message: messages.invalidOption(attr, validLabels)
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function createMultiselectValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
14
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
15
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
16
|
+
return zod.z.array(
|
|
17
|
+
zod.z.enum(validValues, {
|
|
18
|
+
message: messages.invalidOption(attr, validLabels)
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
function createStatusValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
23
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
24
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
25
|
+
return zod.z.enum(validValues, {
|
|
26
|
+
message: messages.invalidOption(attr, validLabels)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.createMultiselectValidator = createMultiselectValidator;
|
|
31
|
+
exports.createSelectValidator = createSelectValidator;
|
|
32
|
+
exports.createStatusValidator = createStatusValidator;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { createCheckboxValidator } from './chunk-TMYBEXBT.mjs';
|
|
2
|
+
import { createDateValidator } from './chunk-4KRLCWJM.mjs';
|
|
3
|
+
import { createNumberValidator } from './chunk-KNYZH2WD.mjs';
|
|
4
|
+
import { createRatingValidator } from './chunk-RUYUFXNW.mjs';
|
|
5
|
+
import { createTextAreaValidator, createTextValidator } from './chunk-KWVCGQ7N.mjs';
|
|
6
|
+
import { createRichtextValidator } from './chunk-BLXQGQLK.mjs';
|
|
7
|
+
import { createMultiselectValidator, createSelectValidator, createStatusValidator } from './chunk-7HFZ7MVR.mjs';
|
|
8
|
+
import { createUserValidator } from './chunk-ITTQ4FGR.mjs';
|
|
9
|
+
import { createFormulaValidator } from './chunk-IRTRJH37.mjs';
|
|
10
|
+
import { createRollupValidator } from './chunk-OLJLCVNY.mjs';
|
|
11
|
+
import { createCurrencyValidator } from './chunk-AYCSVKI2.mjs';
|
|
12
|
+
import { createFileValidator } from './chunk-4TC27SMF.mjs';
|
|
13
|
+
import { createLocationValidator } from './chunk-WV4V64D5.mjs';
|
|
14
|
+
import { createPhoneValidator } from './chunk-5XL3JP73.mjs';
|
|
15
|
+
import { createRelationValidator } from './chunk-R4CJHCC4.mjs';
|
|
16
|
+
import { DEFAULT_VALIDATION_MESSAGES, formatZodErrors } from './chunk-UQVW4KPP.mjs';
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
|
|
19
|
+
function isEmptyValue(value) {
|
|
20
|
+
if (value === null || value === void 0) return true;
|
|
21
|
+
if (typeof value === "string" && value.trim() === "") return true;
|
|
22
|
+
if (value instanceof Date) return false;
|
|
23
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
24
|
+
return Object.values(value).every(
|
|
25
|
+
(v) => v === null || v === void 0 || typeof v === "string" && v.trim() === ""
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
function withEmptyToNull(validator) {
|
|
31
|
+
return z.preprocess((val) => isEmptyValue(val) ? null : val, validator.nullish());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/validation/object/attribute-factory.ts
|
|
35
|
+
function createAttributeValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
36
|
+
switch (attr.type) {
|
|
37
|
+
case "text":
|
|
38
|
+
return createTextValidator(attr, messages);
|
|
39
|
+
case "textarea":
|
|
40
|
+
return createTextAreaValidator(attr, messages);
|
|
41
|
+
case "richtext":
|
|
42
|
+
return createRichtextValidator(attr, messages);
|
|
43
|
+
case "number":
|
|
44
|
+
return createNumberValidator(attr, messages);
|
|
45
|
+
case "checkbox":
|
|
46
|
+
return createCheckboxValidator(attr, messages);
|
|
47
|
+
case "date":
|
|
48
|
+
return createDateValidator(attr, messages);
|
|
49
|
+
case "phone":
|
|
50
|
+
return createPhoneValidator(attr, messages);
|
|
51
|
+
case "currency":
|
|
52
|
+
return createCurrencyValidator(attr, messages);
|
|
53
|
+
case "status":
|
|
54
|
+
return createStatusValidator(attr, messages);
|
|
55
|
+
case "location":
|
|
56
|
+
return createLocationValidator(attr, messages);
|
|
57
|
+
case "select":
|
|
58
|
+
return createSelectValidator(attr, messages);
|
|
59
|
+
case "multiselect":
|
|
60
|
+
return createMultiselectValidator(attr, messages);
|
|
61
|
+
case "file":
|
|
62
|
+
return createFileValidator(attr, messages);
|
|
63
|
+
case "user":
|
|
64
|
+
return createUserValidator(attr, messages);
|
|
65
|
+
case "relation":
|
|
66
|
+
return createRelationValidator(attr, messages);
|
|
67
|
+
case "rating":
|
|
68
|
+
return createRatingValidator(attr, messages);
|
|
69
|
+
case "formula":
|
|
70
|
+
return createFormulaValidator(attr, messages);
|
|
71
|
+
case "rollup":
|
|
72
|
+
return createRollupValidator(attr, messages);
|
|
73
|
+
default:
|
|
74
|
+
return z.unknown();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function createFormAttributeValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
78
|
+
const validator = createAttributeValidator(attr, messages);
|
|
79
|
+
if (!attr.required) {
|
|
80
|
+
return withEmptyToNull(validator);
|
|
81
|
+
}
|
|
82
|
+
return validator;
|
|
83
|
+
}
|
|
84
|
+
var objectValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
85
|
+
var draftValidatorCache = /* @__PURE__ */ new WeakMap();
|
|
86
|
+
function createObjectValidator(objectDef) {
|
|
87
|
+
const cached = objectValidatorCache.get(objectDef);
|
|
88
|
+
if (cached) return cached;
|
|
89
|
+
const shape = {};
|
|
90
|
+
for (const attr of objectDef.attributes) {
|
|
91
|
+
const validator = createAttributeValidator(attr);
|
|
92
|
+
shape[attr.name] = attr.required ? validator : withEmptyToNull(validator);
|
|
93
|
+
}
|
|
94
|
+
const schema = z.object(shape).passthrough();
|
|
95
|
+
objectValidatorCache.set(objectDef, schema);
|
|
96
|
+
return schema;
|
|
97
|
+
}
|
|
98
|
+
function createDraftValidator(objectDef) {
|
|
99
|
+
const cached = draftValidatorCache.get(objectDef);
|
|
100
|
+
if (cached) return cached;
|
|
101
|
+
const shape = {};
|
|
102
|
+
for (const attr of objectDef.attributes) {
|
|
103
|
+
const validator = createAttributeValidator(attr);
|
|
104
|
+
shape[attr.name] = withEmptyToNull(validator);
|
|
105
|
+
}
|
|
106
|
+
const schema = z.object(shape).passthrough();
|
|
107
|
+
draftValidatorCache.set(objectDef, schema);
|
|
108
|
+
return schema;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/validation/object/helpers.ts
|
|
112
|
+
function validateAttribute(attr, value) {
|
|
113
|
+
const validator = createAttributeValidator(attr);
|
|
114
|
+
if (!attr.required && (value === void 0 || value === null)) {
|
|
115
|
+
return { success: true, data: { [attr.name]: value } };
|
|
116
|
+
}
|
|
117
|
+
const result = validator.safeParse(value);
|
|
118
|
+
if (result.success) {
|
|
119
|
+
return {
|
|
120
|
+
success: true,
|
|
121
|
+
data: { [attr.name]: result.data }
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
success: false,
|
|
126
|
+
errors: formatZodErrors(result.error).map((err) => ({
|
|
127
|
+
path: [attr.name, ...err.path],
|
|
128
|
+
message: err.message
|
|
129
|
+
}))
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function validateObject(objectDef, data) {
|
|
133
|
+
const validator = createObjectValidator(objectDef);
|
|
134
|
+
const result = validator.safeParse(data);
|
|
135
|
+
if (result.success) {
|
|
136
|
+
return {
|
|
137
|
+
success: true,
|
|
138
|
+
data: result.data
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
success: false,
|
|
143
|
+
errors: formatZodErrors(result.error)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function validateObjectOrThrow(objectDef, data) {
|
|
147
|
+
const result = validateObject(objectDef, data);
|
|
148
|
+
if (!result.success) {
|
|
149
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
150
|
+
throw new Error(`Validation failed for ${objectDef.label}:
|
|
151
|
+
${errorMessages}`);
|
|
152
|
+
}
|
|
153
|
+
return result.data;
|
|
154
|
+
}
|
|
155
|
+
function validateDraft(objectDef, data) {
|
|
156
|
+
const validator = createDraftValidator(objectDef);
|
|
157
|
+
const result = validator.safeParse(data);
|
|
158
|
+
if (result.success) {
|
|
159
|
+
return {
|
|
160
|
+
success: true,
|
|
161
|
+
data: result.data
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
success: false,
|
|
166
|
+
errors: formatZodErrors(result.error)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function validateDraftOrThrow(objectDef, data) {
|
|
170
|
+
const result = validateDraft(objectDef, data);
|
|
171
|
+
if (!result.success) {
|
|
172
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
173
|
+
throw new Error(`Draft validation failed for ${objectDef.label}:
|
|
174
|
+
${errorMessages}`);
|
|
175
|
+
}
|
|
176
|
+
return result.data;
|
|
177
|
+
}
|
|
178
|
+
function isValuePresent(value) {
|
|
179
|
+
if (value === void 0 || value === null) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
function getMissingRequiredAttributes(objectDef, data) {
|
|
188
|
+
const missing = [];
|
|
189
|
+
for (const attr of objectDef.attributes) {
|
|
190
|
+
if (attr.required && !isValuePresent(data[attr.name])) {
|
|
191
|
+
missing.push(attr);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return missing;
|
|
195
|
+
}
|
|
196
|
+
function isRecordComplete(objectDef, data) {
|
|
197
|
+
const missing = getMissingRequiredAttributes(objectDef, data);
|
|
198
|
+
if (missing.length > 0) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
const validation = validateObject(objectDef, data);
|
|
202
|
+
return validation.success;
|
|
203
|
+
}
|
|
204
|
+
function computeRecordStatus(objectDef, data) {
|
|
205
|
+
return isRecordComplete(objectDef, data) ? "complete" : "draft";
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { computeRecordStatus, createAttributeValidator, createDraftValidator, createFormAttributeValidator, createObjectValidator, getMissingRequiredAttributes, isRecordComplete, validateAttribute, validateDraft, validateDraftOrThrow, validateObject, validateObjectOrThrow };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { getCountryByIso3, isValidIso3, stripTrunkPrefix } from '@stndrds/constants';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
5
|
+
|
|
6
|
+
function digitsOnly(input) {
|
|
7
|
+
return input.replace(/\D/g, "");
|
|
8
|
+
}
|
|
9
|
+
function getIso2(iso3) {
|
|
10
|
+
const country = getCountryByIso3(iso3);
|
|
11
|
+
return country?.iso2;
|
|
12
|
+
}
|
|
13
|
+
function normalizePhoneNumber(phoneNumber, countryIso3) {
|
|
14
|
+
if (!phoneNumber) return "";
|
|
15
|
+
const iso2 = getIso2(countryIso3);
|
|
16
|
+
if (!iso2) {
|
|
17
|
+
return digitsOnly(phoneNumber);
|
|
18
|
+
}
|
|
19
|
+
const parsed = parsePhoneNumberFromString(phoneNumber, iso2);
|
|
20
|
+
if (parsed) {
|
|
21
|
+
return parsed.nationalNumber;
|
|
22
|
+
}
|
|
23
|
+
const country = getCountryByIso3(countryIso3);
|
|
24
|
+
const digits = digitsOnly(phoneNumber);
|
|
25
|
+
if (country) {
|
|
26
|
+
const callingCode = country.phoneCode.replace("+", "");
|
|
27
|
+
const withoutCC = digits.startsWith(callingCode) ? digits.slice(callingCode.length) : digits;
|
|
28
|
+
return stripTrunkPrefix(withoutCC, country.phoneTrunkPrefix ?? "");
|
|
29
|
+
}
|
|
30
|
+
return digits;
|
|
31
|
+
}
|
|
32
|
+
function validatePhoneNumber(phoneNumber, countryIso3) {
|
|
33
|
+
if (!phoneNumber) return false;
|
|
34
|
+
const iso2 = getIso2(countryIso3);
|
|
35
|
+
if (!iso2) return false;
|
|
36
|
+
const country = getCountryByIso3(countryIso3);
|
|
37
|
+
if (!country) return false;
|
|
38
|
+
const trunkPrefix = country.phoneTrunkPrefix ?? "";
|
|
39
|
+
const digits = digitsOnly(phoneNumber);
|
|
40
|
+
const withTrunk = trunkPrefix + digits;
|
|
41
|
+
const parsed = parsePhoneNumberFromString(withTrunk, iso2);
|
|
42
|
+
if (parsed?.isValid()) return true;
|
|
43
|
+
const parsedDirect = parsePhoneNumberFromString(phoneNumber, iso2);
|
|
44
|
+
if (parsedDirect?.isValid()) return true;
|
|
45
|
+
const maxDigits = country.phoneMaxDigits ?? 15;
|
|
46
|
+
const expectedWithoutTrunk = maxDigits - trunkPrefix.length;
|
|
47
|
+
return digits.length === expectedWithoutTrunk || digits.length === maxDigits;
|
|
48
|
+
}
|
|
49
|
+
function formatPhoneForDisplay(phone) {
|
|
50
|
+
if (!phone.phoneNumber) return "";
|
|
51
|
+
const iso2 = getIso2(phone.countryCode);
|
|
52
|
+
const country = getCountryByIso3(phone.countryCode);
|
|
53
|
+
const dial = country?.phoneCode ?? "";
|
|
54
|
+
if (!iso2) {
|
|
55
|
+
return `${dial} ${phone.phoneNumber}`.trim();
|
|
56
|
+
}
|
|
57
|
+
const e164 = `${dial}${digitsOnly(phone.phoneNumber)}`;
|
|
58
|
+
const parsed = parsePhoneNumberFromString(e164);
|
|
59
|
+
if (parsed) {
|
|
60
|
+
return parsed.formatInternational();
|
|
61
|
+
}
|
|
62
|
+
return `${dial} ${phone.phoneNumber}`.trim();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/validation/complex/phone.ts
|
|
66
|
+
function createPhoneValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
67
|
+
return z.object(
|
|
68
|
+
{
|
|
69
|
+
countryCode: z.string().length(3),
|
|
70
|
+
phoneNumber: z.string().min(1)
|
|
71
|
+
},
|
|
72
|
+
{ message: messages.invalidPhone(attr) }
|
|
73
|
+
).refine((val) => isValidIso3(val.countryCode), {
|
|
74
|
+
message: "Invalid country code"
|
|
75
|
+
}).transform((val) => ({
|
|
76
|
+
countryCode: val.countryCode,
|
|
77
|
+
phoneNumber: normalizePhoneNumber(val.phoneNumber, val.countryCode)
|
|
78
|
+
})).refine((val) => validatePhoneNumber(val.phoneNumber, val.countryCode), {
|
|
79
|
+
message: messages.invalidPhone(attr)
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { createPhoneValidator, formatPhoneForDisplay };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createSelectValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
6
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
7
|
+
return z.enum(validValues, {
|
|
8
|
+
message: messages.invalidOption(attr, validLabels)
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function createMultiselectValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
12
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
13
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
14
|
+
return z.array(
|
|
15
|
+
z.enum(validValues, {
|
|
16
|
+
message: messages.invalidOption(attr, validLabels)
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
function createStatusValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
21
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
22
|
+
const validLabels = attr.options.map((opt) => opt.label);
|
|
23
|
+
return z.enum(validValues, {
|
|
24
|
+
message: messages.invalidOption(attr, validLabels)
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { createMultiselectValidator, createSelectValidator, createStatusValidator };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function createRollupValidator(_attr, _messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
7
|
+
return zod.z.unknown();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createRollupValidator = createRollupValidator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createCurrencyValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
return z.object(
|
|
6
|
+
{
|
|
7
|
+
code: z.string().length(3),
|
|
8
|
+
value: z.number().min(0)
|
|
9
|
+
},
|
|
10
|
+
{ message: messages.invalidCurrency(attr) }
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { createCurrencyValidator };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createRichtextValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
return z.string({
|
|
6
|
+
message: messages.invalidRichtext(attr)
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { createRichtextValidator };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function createRatingValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
7
|
+
let schema = zod.z.number().min(0);
|
|
8
|
+
if (attr.max !== void 0) {
|
|
9
|
+
schema = schema.max(attr.max, messages.maxValue(attr, attr.max));
|
|
10
|
+
}
|
|
11
|
+
return schema;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.createRatingValidator = createRatingValidator;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/utils.ts
|
|
4
|
+
function asTenantId(id) {
|
|
5
|
+
return id;
|
|
6
|
+
}
|
|
7
|
+
function asUserId(id) {
|
|
8
|
+
return id;
|
|
9
|
+
}
|
|
10
|
+
function generateId() {
|
|
11
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
12
|
+
return crypto.randomUUID();
|
|
13
|
+
}
|
|
14
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
15
|
+
const r = Math.random() * 16 | 0;
|
|
16
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
17
|
+
return v.toString(16);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function indexBy(items, keyFn) {
|
|
21
|
+
return new Map(items.map((item) => [keyFn(item), item]));
|
|
22
|
+
}
|
|
23
|
+
function deepEqual(a, b) {
|
|
24
|
+
if (a === b) return true;
|
|
25
|
+
if (a == null || b == null) return false;
|
|
26
|
+
if (typeof a !== typeof b) return false;
|
|
27
|
+
if (Array.isArray(a)) {
|
|
28
|
+
if (!Array.isArray(b)) return false;
|
|
29
|
+
if (a.length !== b.length) return false;
|
|
30
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
31
|
+
}
|
|
32
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
33
|
+
const aObj = a;
|
|
34
|
+
const bObj = b;
|
|
35
|
+
const aKeys = Object.keys(aObj);
|
|
36
|
+
const bKeys = Object.keys(bObj);
|
|
37
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
38
|
+
return aKeys.every((key) => key in bObj && deepEqual(aObj[key], bObj[key]));
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
exports.asTenantId = asTenantId;
|
|
44
|
+
exports.asUserId = asUserId;
|
|
45
|
+
exports.deepEqual = deepEqual;
|
|
46
|
+
exports.generateId = generateId;
|
|
47
|
+
exports.indexBy = indexBy;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function createCurrencyValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
7
|
+
return zod.z.object(
|
|
8
|
+
{
|
|
9
|
+
code: zod.z.string().length(3),
|
|
10
|
+
value: zod.z.number().min(0)
|
|
11
|
+
},
|
|
12
|
+
{ message: messages.invalidCurrency(attr) }
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.createCurrencyValidator = createCurrencyValidator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createUserValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
const uuidSchema = z.uuid({
|
|
6
|
+
message: messages.invalidId(attr)
|
|
7
|
+
});
|
|
8
|
+
if (attr.multiple) {
|
|
9
|
+
return z.array(uuidSchema);
|
|
10
|
+
}
|
|
11
|
+
return uuidSchema;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { createUserValidator };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkT225DB55_js = require('./chunk-T225DB55.js');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
|
|
6
|
+
function createDateValidator(attr, messages = chunkT225DB55_js.DEFAULT_VALIDATION_MESSAGES) {
|
|
7
|
+
return zod.z.coerce.date({ message: messages.invalidDate(attr) });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createDateValidator = createDateValidator;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DEFAULT_VALIDATION_MESSAGES } from './chunk-UQVW4KPP.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function createNumberValidator(attr, messages = DEFAULT_VALIDATION_MESSAGES) {
|
|
5
|
+
let schema = z.number();
|
|
6
|
+
if (attr.min !== void 0) {
|
|
7
|
+
schema = schema.min(attr.min, messages.minValue(attr, attr.min));
|
|
8
|
+
}
|
|
9
|
+
if (attr.max !== void 0) {
|
|
10
|
+
schema = schema.max(attr.max, messages.maxValue(attr, attr.max));
|
|
11
|
+
}
|
|
12
|
+
if (attr.unit === "integer") {
|
|
13
|
+
schema = schema.int(messages.mustBeInteger(attr));
|
|
14
|
+
}
|
|
15
|
+
return schema;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { createNumberValidator };
|