@attrx/role-morphic 0.1.0 → 0.2.1
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/README.md +151 -84
- package/dist/cast.d.mts +678 -0
- package/dist/cast.d.ts +678 -0
- package/dist/cast.js +3569 -0
- package/dist/cast.js.map +1 -0
- package/dist/cast.mjs +3536 -0
- package/dist/cast.mjs.map +1 -0
- package/dist/constants-BZdBwuvJ.d.mts +168 -0
- package/dist/constants-BZdBwuvJ.d.ts +168 -0
- package/dist/convert.d.mts +1157 -0
- package/dist/convert.d.ts +1157 -0
- package/dist/convert.js +2244 -0
- package/dist/convert.js.map +1 -0
- package/dist/convert.mjs +2148 -0
- package/dist/convert.mjs.map +1 -0
- package/dist/format-Be15LzfS.d.ts +668 -0
- package/dist/format-o4Y3jPH-.d.mts +668 -0
- package/dist/format.d.mts +3 -0
- package/dist/format.d.ts +3 -0
- package/dist/format.js +2303 -0
- package/dist/format.js.map +1 -0
- package/dist/format.mjs +2286 -0
- package/dist/format.mjs.map +1 -0
- package/dist/index.d.mts +200 -601
- package/dist/index.d.ts +200 -601
- package/dist/index.js +5536 -2493
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5349 -2494
- package/dist/index.mjs.map +1 -1
- package/dist/types-mbeS1e-k.d.mts +312 -0
- package/dist/types-mbeS1e-k.d.ts +312 -0
- package/dist/validate.d.mts +884 -0
- package/dist/validate.d.ts +884 -0
- package/dist/validate.js +713 -0
- package/dist/validate.js.map +1 -0
- package/dist/validate.mjs +669 -0
- package/dist/validate.mjs.map +1 -0
- package/package.json +21 -1
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
// src/roles/area/validate.ts
|
|
2
|
+
var AREA_VALIDATION_CONFIG = {
|
|
3
|
+
min: 0,
|
|
4
|
+
minError: "Area cannot be negative"
|
|
5
|
+
};
|
|
6
|
+
function validateArea(value, config = AREA_VALIDATION_CONFIG) {
|
|
7
|
+
const errors = [];
|
|
8
|
+
if (typeof value !== "number") {
|
|
9
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
10
|
+
}
|
|
11
|
+
if (!Number.isFinite(value)) {
|
|
12
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
13
|
+
}
|
|
14
|
+
if (config.min !== void 0 && value < config.min) {
|
|
15
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
16
|
+
errors.push(msg);
|
|
17
|
+
}
|
|
18
|
+
if (config.max !== void 0 && value > config.max) {
|
|
19
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
20
|
+
errors.push(msg);
|
|
21
|
+
}
|
|
22
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
23
|
+
errors.push("Value must be an integer");
|
|
24
|
+
}
|
|
25
|
+
return { valid: errors.length === 0, errors };
|
|
26
|
+
}
|
|
27
|
+
function isValidArea(value, config = AREA_VALIDATION_CONFIG) {
|
|
28
|
+
return validateArea(value, config).valid;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/roles/length/validate.ts
|
|
32
|
+
var LENGTH_VALIDATION_CONFIG = {
|
|
33
|
+
min: 0,
|
|
34
|
+
minError: "Length cannot be negative"
|
|
35
|
+
};
|
|
36
|
+
function validateLength(value, config = LENGTH_VALIDATION_CONFIG) {
|
|
37
|
+
const errors = [];
|
|
38
|
+
if (typeof value !== "number") {
|
|
39
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
40
|
+
}
|
|
41
|
+
if (!Number.isFinite(value)) {
|
|
42
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
43
|
+
}
|
|
44
|
+
if (config.min !== void 0 && value < config.min) {
|
|
45
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
46
|
+
errors.push(msg);
|
|
47
|
+
}
|
|
48
|
+
if (config.max !== void 0 && value > config.max) {
|
|
49
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
50
|
+
errors.push(msg);
|
|
51
|
+
}
|
|
52
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
53
|
+
errors.push("Value must be an integer");
|
|
54
|
+
}
|
|
55
|
+
return { valid: errors.length === 0, errors };
|
|
56
|
+
}
|
|
57
|
+
function isValidLength(value, config = LENGTH_VALIDATION_CONFIG) {
|
|
58
|
+
return validateLength(value, config).valid;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/roles/mass/validate.ts
|
|
62
|
+
var MASS_VALIDATION_CONFIG = {
|
|
63
|
+
min: 0,
|
|
64
|
+
minError: "Mass cannot be negative"
|
|
65
|
+
};
|
|
66
|
+
function validateMass(value, config = MASS_VALIDATION_CONFIG) {
|
|
67
|
+
const errors = [];
|
|
68
|
+
if (typeof value !== "number") {
|
|
69
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
70
|
+
}
|
|
71
|
+
if (!Number.isFinite(value)) {
|
|
72
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
73
|
+
}
|
|
74
|
+
if (config.min !== void 0 && value < config.min) {
|
|
75
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
76
|
+
errors.push(msg);
|
|
77
|
+
}
|
|
78
|
+
if (config.max !== void 0 && value > config.max) {
|
|
79
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
80
|
+
errors.push(msg);
|
|
81
|
+
}
|
|
82
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
83
|
+
errors.push("Value must be an integer");
|
|
84
|
+
}
|
|
85
|
+
return { valid: errors.length === 0, errors };
|
|
86
|
+
}
|
|
87
|
+
function isValidMass(value, config = MASS_VALIDATION_CONFIG) {
|
|
88
|
+
return validateMass(value, config).valid;
|
|
89
|
+
}
|
|
90
|
+
var TEMPERATURE_CONSTANTS = {
|
|
91
|
+
/** Zero absoluto em Celsius */
|
|
92
|
+
ABSOLUTE_ZERO_CELSIUS: -273.15,
|
|
93
|
+
/** Zero absoluto em Fahrenheit */
|
|
94
|
+
ABSOLUTE_ZERO_FAHRENHEIT: -459.67};
|
|
95
|
+
|
|
96
|
+
// src/roles/temperature/validate.ts
|
|
97
|
+
var ABSOLUTE_ZERO = {
|
|
98
|
+
celsius: TEMPERATURE_CONSTANTS.ABSOLUTE_ZERO_CELSIUS,
|
|
99
|
+
fahrenheit: TEMPERATURE_CONSTANTS.ABSOLUTE_ZERO_FAHRENHEIT,
|
|
100
|
+
kelvin: 0,
|
|
101
|
+
rankine: 0
|
|
102
|
+
};
|
|
103
|
+
var UNIT_SYMBOLS = {
|
|
104
|
+
celsius: "\xB0C",
|
|
105
|
+
fahrenheit: "\xB0F",
|
|
106
|
+
kelvin: "K",
|
|
107
|
+
rankine: "\xB0R"
|
|
108
|
+
};
|
|
109
|
+
function validateTemperature(value, unit = "celsius", config = {}) {
|
|
110
|
+
const errors = [];
|
|
111
|
+
if (typeof value !== "number") {
|
|
112
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
113
|
+
}
|
|
114
|
+
if (!Number.isFinite(value)) {
|
|
115
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
116
|
+
}
|
|
117
|
+
if (!config.allowBelowAbsoluteZero) {
|
|
118
|
+
const absoluteZero = ABSOLUTE_ZERO[unit];
|
|
119
|
+
if (value < absoluteZero) {
|
|
120
|
+
const symbol = UNIT_SYMBOLS[unit];
|
|
121
|
+
errors.push(
|
|
122
|
+
`Temperature cannot be below absolute zero (${absoluteZero}${symbol})`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (config.max !== void 0 && value > config.max) {
|
|
127
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
128
|
+
errors.push(msg);
|
|
129
|
+
}
|
|
130
|
+
return { valid: errors.length === 0, errors };
|
|
131
|
+
}
|
|
132
|
+
function isValidTemperature(value, unit = "celsius", config = {}) {
|
|
133
|
+
return validateTemperature(value, unit, config).valid;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/roles/volume/validate.ts
|
|
137
|
+
var VOLUME_VALIDATION_CONFIG = {
|
|
138
|
+
min: 0,
|
|
139
|
+
minError: "Volume cannot be negative"
|
|
140
|
+
};
|
|
141
|
+
function validateVolume(value, config = VOLUME_VALIDATION_CONFIG) {
|
|
142
|
+
const errors = [];
|
|
143
|
+
if (typeof value !== "number") {
|
|
144
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
145
|
+
}
|
|
146
|
+
if (!Number.isFinite(value)) {
|
|
147
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
148
|
+
}
|
|
149
|
+
if (config.min !== void 0 && value < config.min) {
|
|
150
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
151
|
+
errors.push(msg);
|
|
152
|
+
}
|
|
153
|
+
if (config.max !== void 0 && value > config.max) {
|
|
154
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
155
|
+
errors.push(msg);
|
|
156
|
+
}
|
|
157
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
158
|
+
errors.push("Value must be an integer");
|
|
159
|
+
}
|
|
160
|
+
return { valid: errors.length === 0, errors };
|
|
161
|
+
}
|
|
162
|
+
function isValidVolume(value, config = VOLUME_VALIDATION_CONFIG) {
|
|
163
|
+
return validateVolume(value, config).valid;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/roles/speed/validate.ts
|
|
167
|
+
var SPEED_VALIDATION_CONFIG = {};
|
|
168
|
+
function validateSpeed(value, config = SPEED_VALIDATION_CONFIG) {
|
|
169
|
+
const errors = [];
|
|
170
|
+
if (typeof value !== "number") {
|
|
171
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
172
|
+
}
|
|
173
|
+
if (!Number.isFinite(value)) {
|
|
174
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
175
|
+
}
|
|
176
|
+
if (config.min !== void 0 && value < config.min) {
|
|
177
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
178
|
+
errors.push(msg);
|
|
179
|
+
}
|
|
180
|
+
if (config.max !== void 0 && value > config.max) {
|
|
181
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
182
|
+
errors.push(msg);
|
|
183
|
+
}
|
|
184
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
185
|
+
errors.push("Value must be an integer");
|
|
186
|
+
}
|
|
187
|
+
return { valid: errors.length === 0, errors };
|
|
188
|
+
}
|
|
189
|
+
function isValidSpeed(value, config = SPEED_VALIDATION_CONFIG) {
|
|
190
|
+
return validateSpeed(value, config).valid;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/roles/energy/validate.ts
|
|
194
|
+
var ENERGY_VALIDATION_CONFIG = {
|
|
195
|
+
min: 0,
|
|
196
|
+
minError: "Energy cannot be negative"
|
|
197
|
+
};
|
|
198
|
+
function validateEnergy(value, config = ENERGY_VALIDATION_CONFIG) {
|
|
199
|
+
const errors = [];
|
|
200
|
+
if (typeof value !== "number") {
|
|
201
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
202
|
+
}
|
|
203
|
+
if (!Number.isFinite(value)) {
|
|
204
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
205
|
+
}
|
|
206
|
+
if (config.min !== void 0 && value < config.min) {
|
|
207
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
208
|
+
errors.push(msg);
|
|
209
|
+
}
|
|
210
|
+
if (config.max !== void 0 && value > config.max) {
|
|
211
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
212
|
+
errors.push(msg);
|
|
213
|
+
}
|
|
214
|
+
if (config.integer && !Number.isInteger(value)) {
|
|
215
|
+
errors.push("Value must be an integer");
|
|
216
|
+
}
|
|
217
|
+
return { valid: errors.length === 0, errors };
|
|
218
|
+
}
|
|
219
|
+
function isValidEnergy(value, config = ENERGY_VALIDATION_CONFIG) {
|
|
220
|
+
return validateEnergy(value, config).valid;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/roles/power/validate.ts
|
|
224
|
+
var POWER_VALIDATION_CONFIG = {
|
|
225
|
+
min: 0,
|
|
226
|
+
minError: "Power cannot be negative",
|
|
227
|
+
allowNegative: false
|
|
228
|
+
};
|
|
229
|
+
function validatePower(value, config = POWER_VALIDATION_CONFIG) {
|
|
230
|
+
const errors = [];
|
|
231
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
232
|
+
return {
|
|
233
|
+
valid: false,
|
|
234
|
+
errors: ["Value must be a finite number"]
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const effectiveConfig = { ...POWER_VALIDATION_CONFIG, ...config };
|
|
238
|
+
if (!effectiveConfig.allowNegative && value < 0) {
|
|
239
|
+
errors.push(effectiveConfig.minError || "Power cannot be negative");
|
|
240
|
+
}
|
|
241
|
+
if (effectiveConfig.allowNegative && effectiveConfig.min !== void 0 && value < effectiveConfig.min) {
|
|
242
|
+
errors.push(effectiveConfig.minError || `Value must be at least ${effectiveConfig.min}`);
|
|
243
|
+
}
|
|
244
|
+
if (effectiveConfig.max !== void 0 && value > effectiveConfig.max) {
|
|
245
|
+
errors.push(effectiveConfig.maxError || `Value must be at most ${effectiveConfig.max}`);
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
valid: errors.length === 0,
|
|
249
|
+
errors
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function isValidPower(value, config) {
|
|
253
|
+
return validatePower(value, config).valid;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/roles/pressure/validate.ts
|
|
257
|
+
var PRESSURE_VALIDATION_CONFIG = {
|
|
258
|
+
min: 0,
|
|
259
|
+
minError: "Pressure cannot be negative",
|
|
260
|
+
allowNegative: false
|
|
261
|
+
};
|
|
262
|
+
function validatePressure(value, config = PRESSURE_VALIDATION_CONFIG) {
|
|
263
|
+
const errors = [];
|
|
264
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
265
|
+
return {
|
|
266
|
+
valid: false,
|
|
267
|
+
errors: ["Value must be a finite number"]
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const effectiveConfig = { ...PRESSURE_VALIDATION_CONFIG, ...config };
|
|
271
|
+
if (!effectiveConfig.allowNegative && value < 0) {
|
|
272
|
+
errors.push(effectiveConfig.minError || "Pressure cannot be negative");
|
|
273
|
+
}
|
|
274
|
+
if (effectiveConfig.allowNegative && effectiveConfig.min !== void 0 && value < effectiveConfig.min) {
|
|
275
|
+
errors.push(effectiveConfig.minError || `Value must be at least ${effectiveConfig.min}`);
|
|
276
|
+
}
|
|
277
|
+
if (effectiveConfig.max !== void 0 && value > effectiveConfig.max) {
|
|
278
|
+
errors.push(effectiveConfig.maxError || `Value must be at most ${effectiveConfig.max}`);
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
valid: errors.length === 0,
|
|
282
|
+
errors
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function isValidPressure(value, config) {
|
|
286
|
+
return validatePressure(value, config).valid;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/roles/frequency/validate.ts
|
|
290
|
+
var FREQUENCY_VALIDATION_CONFIG = {
|
|
291
|
+
min: 0,
|
|
292
|
+
minError: "Frequency cannot be negative",
|
|
293
|
+
allowNegative: false
|
|
294
|
+
};
|
|
295
|
+
function validateFrequency(value, config = FREQUENCY_VALIDATION_CONFIG) {
|
|
296
|
+
const errors = [];
|
|
297
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
298
|
+
return {
|
|
299
|
+
valid: false,
|
|
300
|
+
errors: ["Value must be a finite number"]
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
const effectiveConfig = { ...FREQUENCY_VALIDATION_CONFIG, ...config };
|
|
304
|
+
if (!effectiveConfig.allowNegative && value < 0) {
|
|
305
|
+
errors.push(effectiveConfig.minError || "Frequency cannot be negative");
|
|
306
|
+
}
|
|
307
|
+
if (effectiveConfig.allowNegative && effectiveConfig.min !== void 0 && value < effectiveConfig.min) {
|
|
308
|
+
errors.push(effectiveConfig.minError || `Value must be at least ${effectiveConfig.min}`);
|
|
309
|
+
}
|
|
310
|
+
if (effectiveConfig.max !== void 0 && value > effectiveConfig.max) {
|
|
311
|
+
errors.push(effectiveConfig.maxError || `Value must be at most ${effectiveConfig.max}`);
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
valid: errors.length === 0,
|
|
315
|
+
errors
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function isValidFrequency(value, config) {
|
|
319
|
+
return validateFrequency(value, config).valid;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/roles/angle/validate.ts
|
|
323
|
+
var ANGLE_VALIDATION_CONFIG = {};
|
|
324
|
+
function validateAngle(value, config = ANGLE_VALIDATION_CONFIG) {
|
|
325
|
+
const errors = [];
|
|
326
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
327
|
+
return {
|
|
328
|
+
valid: false,
|
|
329
|
+
errors: ["Value must be a finite number"]
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
if (config.min !== void 0 && value < config.min) {
|
|
333
|
+
errors.push(config.minError || `Value must be at least ${config.min}`);
|
|
334
|
+
}
|
|
335
|
+
if (config.max !== void 0 && value > config.max) {
|
|
336
|
+
errors.push(config.maxError || `Value must be at most ${config.max}`);
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
valid: errors.length === 0,
|
|
340
|
+
errors
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function isValidAngle(value, config) {
|
|
344
|
+
return validateAngle(value, config).valid;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// src/roles/time/validate.ts
|
|
348
|
+
var TIME_VALIDATION_CONFIG = {
|
|
349
|
+
min: 0,
|
|
350
|
+
minError: "Time cannot be negative",
|
|
351
|
+
allowNegative: false
|
|
352
|
+
};
|
|
353
|
+
function validateTime(value, config = TIME_VALIDATION_CONFIG) {
|
|
354
|
+
const errors = [];
|
|
355
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
356
|
+
return {
|
|
357
|
+
valid: false,
|
|
358
|
+
errors: ["Value must be a finite number"]
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
const effectiveConfig = { ...TIME_VALIDATION_CONFIG, ...config };
|
|
362
|
+
if (!effectiveConfig.allowNegative && value < 0) {
|
|
363
|
+
errors.push(effectiveConfig.minError || "Time cannot be negative");
|
|
364
|
+
}
|
|
365
|
+
if (effectiveConfig.allowNegative && effectiveConfig.min !== void 0 && value < effectiveConfig.min) {
|
|
366
|
+
errors.push(effectiveConfig.minError || `Value must be at least ${effectiveConfig.min}`);
|
|
367
|
+
}
|
|
368
|
+
if (effectiveConfig.max !== void 0 && value > effectiveConfig.max) {
|
|
369
|
+
errors.push(effectiveConfig.maxError || `Value must be at most ${effectiveConfig.max}`);
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
valid: errors.length === 0,
|
|
373
|
+
errors
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function isValidTime(value, config) {
|
|
377
|
+
return validateTime(value, config).valid;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/roles/digital/validate.ts
|
|
381
|
+
var DIGITAL_VALIDATION_CONFIG = {
|
|
382
|
+
min: 0,
|
|
383
|
+
minError: "Digital storage cannot be negative",
|
|
384
|
+
allowNegative: false
|
|
385
|
+
};
|
|
386
|
+
function validateDigital(value, config = DIGITAL_VALIDATION_CONFIG) {
|
|
387
|
+
const errors = [];
|
|
388
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
389
|
+
return {
|
|
390
|
+
valid: false,
|
|
391
|
+
errors: ["Value must be a finite number"]
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const effectiveConfig = { ...DIGITAL_VALIDATION_CONFIG, ...config };
|
|
395
|
+
if (!effectiveConfig.allowNegative && value < 0) {
|
|
396
|
+
errors.push(effectiveConfig.minError || "Digital storage cannot be negative");
|
|
397
|
+
}
|
|
398
|
+
if (effectiveConfig.allowNegative && effectiveConfig.min !== void 0 && value < effectiveConfig.min) {
|
|
399
|
+
errors.push(effectiveConfig.minError || `Value must be at least ${effectiveConfig.min}`);
|
|
400
|
+
}
|
|
401
|
+
if (effectiveConfig.max !== void 0 && value > effectiveConfig.max) {
|
|
402
|
+
errors.push(effectiveConfig.maxError || `Value must be at most ${effectiveConfig.max}`);
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
valid: errors.length === 0,
|
|
406
|
+
errors
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function isValidDigital(value, config) {
|
|
410
|
+
return validateDigital(value, config).valid;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/roles/color/validate.ts
|
|
414
|
+
var HEX_3_REGEX = /^#?[0-9a-f]{3}$/i;
|
|
415
|
+
var HEX_4_REGEX = /^#?[0-9a-f]{4}$/i;
|
|
416
|
+
var HEX_6_REGEX = /^#?[0-9a-f]{6}$/i;
|
|
417
|
+
var HEX_8_REGEX = /^#?[0-9a-f]{8}$/i;
|
|
418
|
+
var RGB_STRING_REGEX = /^rgba?\s*\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*[\d.]+\s*)?\)$/i;
|
|
419
|
+
var HSL_STRING_REGEX = /^hsla?\s*\(\s*[\d.]+\s*,\s*[\d.]+%?\s*,\s*[\d.]+%?\s*(,\s*[\d.]+\s*)?\)$/i;
|
|
420
|
+
function validateHex(value) {
|
|
421
|
+
const errors = [];
|
|
422
|
+
if (typeof value !== "string") {
|
|
423
|
+
errors.push("Hex color must be a string");
|
|
424
|
+
return { valid: false, errors };
|
|
425
|
+
}
|
|
426
|
+
const trimmed = value.trim();
|
|
427
|
+
if (!HEX_3_REGEX.test(trimmed) && !HEX_4_REGEX.test(trimmed) && !HEX_6_REGEX.test(trimmed) && !HEX_8_REGEX.test(trimmed)) {
|
|
428
|
+
errors.push("Invalid hex color format");
|
|
429
|
+
return { valid: false, errors };
|
|
430
|
+
}
|
|
431
|
+
return { valid: true, errors: [] };
|
|
432
|
+
}
|
|
433
|
+
function validateRgbObject(value) {
|
|
434
|
+
const errors = [];
|
|
435
|
+
if (typeof value !== "object" || value === null) {
|
|
436
|
+
errors.push("RGB object must be an object");
|
|
437
|
+
return { valid: false, errors };
|
|
438
|
+
}
|
|
439
|
+
const obj = value;
|
|
440
|
+
if (typeof obj.r !== "number") {
|
|
441
|
+
errors.push("RGB object must have numeric 'r' property");
|
|
442
|
+
}
|
|
443
|
+
if (typeof obj.g !== "number") {
|
|
444
|
+
errors.push("RGB object must have numeric 'g' property");
|
|
445
|
+
}
|
|
446
|
+
if (typeof obj.b !== "number") {
|
|
447
|
+
errors.push("RGB object must have numeric 'b' property");
|
|
448
|
+
}
|
|
449
|
+
if (errors.length > 0) {
|
|
450
|
+
return { valid: false, errors };
|
|
451
|
+
}
|
|
452
|
+
const { r, g, b, a } = obj;
|
|
453
|
+
if (r < 0 || r > 255) {
|
|
454
|
+
errors.push("RGB 'r' must be between 0 and 255");
|
|
455
|
+
}
|
|
456
|
+
if (g < 0 || g > 255) {
|
|
457
|
+
errors.push("RGB 'g' must be between 0 and 255");
|
|
458
|
+
}
|
|
459
|
+
if (b < 0 || b > 255) {
|
|
460
|
+
errors.push("RGB 'b' must be between 0 and 255");
|
|
461
|
+
}
|
|
462
|
+
if (a !== void 0 && (typeof a !== "number" || a < 0 || a > 1)) {
|
|
463
|
+
errors.push("RGB 'a' must be a number between 0 and 1");
|
|
464
|
+
}
|
|
465
|
+
return { valid: errors.length === 0, errors };
|
|
466
|
+
}
|
|
467
|
+
function validateRgbString(value) {
|
|
468
|
+
const errors = [];
|
|
469
|
+
if (typeof value !== "string") {
|
|
470
|
+
errors.push("RGB string must be a string");
|
|
471
|
+
return { valid: false, errors };
|
|
472
|
+
}
|
|
473
|
+
const trimmed = value.trim();
|
|
474
|
+
if (!RGB_STRING_REGEX.test(trimmed)) {
|
|
475
|
+
errors.push("Invalid RGB string format");
|
|
476
|
+
return { valid: false, errors };
|
|
477
|
+
}
|
|
478
|
+
return { valid: true, errors: [] };
|
|
479
|
+
}
|
|
480
|
+
function validateHslObject(value) {
|
|
481
|
+
const errors = [];
|
|
482
|
+
if (typeof value !== "object" || value === null) {
|
|
483
|
+
errors.push("HSL object must be an object");
|
|
484
|
+
return { valid: false, errors };
|
|
485
|
+
}
|
|
486
|
+
const obj = value;
|
|
487
|
+
if (typeof obj.h !== "number") {
|
|
488
|
+
errors.push("HSL object must have numeric 'h' property");
|
|
489
|
+
}
|
|
490
|
+
if (typeof obj.s !== "number") {
|
|
491
|
+
errors.push("HSL object must have numeric 's' property");
|
|
492
|
+
}
|
|
493
|
+
if (typeof obj.l !== "number") {
|
|
494
|
+
errors.push("HSL object must have numeric 'l' property");
|
|
495
|
+
}
|
|
496
|
+
if (errors.length > 0) {
|
|
497
|
+
return { valid: false, errors };
|
|
498
|
+
}
|
|
499
|
+
const { h, s, l, a } = obj;
|
|
500
|
+
if (h < 0 || h > 360) {
|
|
501
|
+
errors.push("HSL 'h' must be between 0 and 360");
|
|
502
|
+
}
|
|
503
|
+
if (s < 0 || s > 100) {
|
|
504
|
+
errors.push("HSL 's' must be between 0 and 100");
|
|
505
|
+
}
|
|
506
|
+
if (l < 0 || l > 100) {
|
|
507
|
+
errors.push("HSL 'l' must be between 0 and 100");
|
|
508
|
+
}
|
|
509
|
+
if (a !== void 0 && (typeof a !== "number" || a < 0 || a > 1)) {
|
|
510
|
+
errors.push("HSL 'a' must be a number between 0 and 1");
|
|
511
|
+
}
|
|
512
|
+
return { valid: errors.length === 0, errors };
|
|
513
|
+
}
|
|
514
|
+
function validateHslString(value) {
|
|
515
|
+
const errors = [];
|
|
516
|
+
if (typeof value !== "string") {
|
|
517
|
+
errors.push("HSL string must be a string");
|
|
518
|
+
return { valid: false, errors };
|
|
519
|
+
}
|
|
520
|
+
const trimmed = value.trim();
|
|
521
|
+
if (!HSL_STRING_REGEX.test(trimmed)) {
|
|
522
|
+
errors.push("Invalid HSL string format");
|
|
523
|
+
return { valid: false, errors };
|
|
524
|
+
}
|
|
525
|
+
return { valid: true, errors: [] };
|
|
526
|
+
}
|
|
527
|
+
function validateColor(variant, value) {
|
|
528
|
+
switch (variant) {
|
|
529
|
+
case "hex":
|
|
530
|
+
return validateHex(value);
|
|
531
|
+
case "rgb_object":
|
|
532
|
+
return validateRgbObject(value);
|
|
533
|
+
case "rgb_string":
|
|
534
|
+
return validateRgbString(value);
|
|
535
|
+
case "hsl_object":
|
|
536
|
+
return validateHslObject(value);
|
|
537
|
+
case "hsl_string":
|
|
538
|
+
return validateHslString(value);
|
|
539
|
+
default:
|
|
540
|
+
return { valid: false, errors: [`Unknown color variant: ${variant}`] };
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function isValidColorVariant(variant, value) {
|
|
544
|
+
return validateColor(variant, value).valid;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/roles/date/validate.ts
|
|
548
|
+
var ISO_DATE_TIME_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
|
|
549
|
+
var ISO_DATE_ONLY_REGEX = /^\d{4}-\d{2}-\d{2}$/;
|
|
550
|
+
function validateIso(value) {
|
|
551
|
+
const errors = [];
|
|
552
|
+
if (typeof value !== "string") {
|
|
553
|
+
errors.push("ISO date must be a string");
|
|
554
|
+
return { valid: false, errors };
|
|
555
|
+
}
|
|
556
|
+
const trimmed = value.trim();
|
|
557
|
+
if (!ISO_DATE_TIME_REGEX.test(trimmed) && !ISO_DATE_ONLY_REGEX.test(trimmed)) {
|
|
558
|
+
errors.push("Invalid ISO 8601 format");
|
|
559
|
+
return { valid: false, errors };
|
|
560
|
+
}
|
|
561
|
+
const date = new Date(trimmed);
|
|
562
|
+
if (isNaN(date.getTime())) {
|
|
563
|
+
errors.push("Invalid date value");
|
|
564
|
+
return { valid: false, errors };
|
|
565
|
+
}
|
|
566
|
+
return { valid: true, errors: [] };
|
|
567
|
+
}
|
|
568
|
+
function validateTimestamp(value) {
|
|
569
|
+
const errors = [];
|
|
570
|
+
if (typeof value !== "number") {
|
|
571
|
+
errors.push("Timestamp must be a number");
|
|
572
|
+
return { valid: false, errors };
|
|
573
|
+
}
|
|
574
|
+
if (!Number.isFinite(value)) {
|
|
575
|
+
errors.push("Timestamp must be a finite number");
|
|
576
|
+
return { valid: false, errors };
|
|
577
|
+
}
|
|
578
|
+
if (!Number.isInteger(value)) {
|
|
579
|
+
errors.push("Timestamp must be an integer");
|
|
580
|
+
return { valid: false, errors };
|
|
581
|
+
}
|
|
582
|
+
return { valid: true, errors: [] };
|
|
583
|
+
}
|
|
584
|
+
function validateEpoch(value) {
|
|
585
|
+
const errors = [];
|
|
586
|
+
if (typeof value !== "number") {
|
|
587
|
+
errors.push("Epoch must be a number");
|
|
588
|
+
return { valid: false, errors };
|
|
589
|
+
}
|
|
590
|
+
if (!Number.isFinite(value)) {
|
|
591
|
+
errors.push("Epoch must be a finite number");
|
|
592
|
+
return { valid: false, errors };
|
|
593
|
+
}
|
|
594
|
+
if (!Number.isInteger(value)) {
|
|
595
|
+
errors.push("Epoch must be an integer");
|
|
596
|
+
return { valid: false, errors };
|
|
597
|
+
}
|
|
598
|
+
return { valid: true, errors: [] };
|
|
599
|
+
}
|
|
600
|
+
function validateDate(variant, value) {
|
|
601
|
+
switch (variant) {
|
|
602
|
+
case "iso":
|
|
603
|
+
return validateIso(value);
|
|
604
|
+
case "timestamp":
|
|
605
|
+
return validateTimestamp(value);
|
|
606
|
+
case "epoch":
|
|
607
|
+
return validateEpoch(value);
|
|
608
|
+
default:
|
|
609
|
+
return { valid: false, errors: [`Unknown date variant: ${variant}`] };
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function isValidDate(variant, value) {
|
|
613
|
+
return validateDate(variant, value).valid;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// src/roles/currency/validate.ts
|
|
617
|
+
var CURRENCY_VALIDATION_CONFIG = {
|
|
618
|
+
min: 0,
|
|
619
|
+
allowNegative: false,
|
|
620
|
+
minError: "Currency value cannot be negative"
|
|
621
|
+
};
|
|
622
|
+
function validateCurrency(value, config = CURRENCY_VALIDATION_CONFIG) {
|
|
623
|
+
const errors = [];
|
|
624
|
+
if (typeof value !== "number") {
|
|
625
|
+
return { valid: false, errors: ["Value must be a number"] };
|
|
626
|
+
}
|
|
627
|
+
if (!Number.isFinite(value)) {
|
|
628
|
+
return { valid: false, errors: ["Value must be finite"] };
|
|
629
|
+
}
|
|
630
|
+
if (!config.allowNegative && value < 0) {
|
|
631
|
+
const msg = config.minError ?? "Currency value cannot be negative";
|
|
632
|
+
errors.push(msg);
|
|
633
|
+
}
|
|
634
|
+
if (config.allowNegative && config.min !== void 0 && value < config.min) {
|
|
635
|
+
const msg = config.minError ?? `Value must be at least ${config.min}`;
|
|
636
|
+
errors.push(msg);
|
|
637
|
+
}
|
|
638
|
+
if (config.max !== void 0 && value > config.max) {
|
|
639
|
+
const msg = config.maxError ?? `Value must be at most ${config.max}`;
|
|
640
|
+
errors.push(msg);
|
|
641
|
+
}
|
|
642
|
+
if (config.decimals !== void 0) {
|
|
643
|
+
const decimalPlaces = countDecimalPlaces(value);
|
|
644
|
+
if (decimalPlaces > config.decimals) {
|
|
645
|
+
errors.push(
|
|
646
|
+
`Value has too many decimal places (max: ${config.decimals})`
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
return { valid: errors.length === 0, errors };
|
|
651
|
+
}
|
|
652
|
+
function isValidCurrency(value, config = CURRENCY_VALIDATION_CONFIG) {
|
|
653
|
+
return validateCurrency(value, config).valid;
|
|
654
|
+
}
|
|
655
|
+
function countDecimalPlaces(value) {
|
|
656
|
+
if (Number.isInteger(value)) {
|
|
657
|
+
return 0;
|
|
658
|
+
}
|
|
659
|
+
const str = String(value);
|
|
660
|
+
const decimalIndex = str.indexOf(".");
|
|
661
|
+
if (decimalIndex === -1) {
|
|
662
|
+
return 0;
|
|
663
|
+
}
|
|
664
|
+
return str.length - decimalIndex - 1;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
export { AREA_VALIDATION_CONFIG, CURRENCY_VALIDATION_CONFIG, DIGITAL_VALIDATION_CONFIG, ENERGY_VALIDATION_CONFIG, FREQUENCY_VALIDATION_CONFIG, LENGTH_VALIDATION_CONFIG, MASS_VALIDATION_CONFIG, POWER_VALIDATION_CONFIG, PRESSURE_VALIDATION_CONFIG, TIME_VALIDATION_CONFIG, VOLUME_VALIDATION_CONFIG, isValidAngle, isValidArea, isValidColorVariant, isValidCurrency, isValidDate, isValidDigital, isValidEnergy, isValidFrequency, isValidLength, isValidMass, isValidPower, isValidPressure, isValidSpeed, isValidTemperature, isValidTime, isValidVolume, validateAngle, validateArea, validateColor, validateCurrency, validateDate, validateDigital, validateEnergy, validateFrequency, validateLength, validateMass, validatePower, validatePressure, validateSpeed, validateTemperature, validateTime, validateVolume };
|
|
668
|
+
//# sourceMappingURL=validate.mjs.map
|
|
669
|
+
//# sourceMappingURL=validate.mjs.map
|