@wildo-ai/zod-decorators 1.0.0
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/esm/__mocks__/mime.d.ts +12 -0
- package/dist/esm/__mocks__/mime.d.ts.map +1 -0
- package/dist/esm/__mocks__/mime.js +75 -0
- package/dist/esm/__mocks__/mime.js.map +1 -0
- package/dist/esm/commons.d.ts +26 -0
- package/dist/esm/commons.d.ts.map +1 -0
- package/dist/esm/commons.js +41 -0
- package/dist/esm/commons.js.map +1 -0
- package/dist/esm/currencies.shared.schemas.d.ts +208 -0
- package/dist/esm/currencies.shared.schemas.d.ts.map +1 -0
- package/dist/esm/currencies.shared.schemas.js +228 -0
- package/dist/esm/currencies.shared.schemas.js.map +1 -0
- package/dist/esm/files/agent-file-formats.d.ts +99 -0
- package/dist/esm/files/agent-file-formats.d.ts.map +1 -0
- package/dist/esm/files/agent-file-formats.js +163 -0
- package/dist/esm/files/agent-file-formats.js.map +1 -0
- package/dist/esm/files/extension-nature-map.d.ts +12 -0
- package/dist/esm/files/extension-nature-map.d.ts.map +1 -0
- package/dist/esm/files/extension-nature-map.js +63 -0
- package/dist/esm/files/extension-nature-map.js.map +1 -0
- package/dist/esm/files/file-extensions.enum.d.ts +58 -0
- package/dist/esm/files/file-extensions.enum.d.ts.map +1 -0
- package/dist/esm/files/file-extensions.enum.js +65 -0
- package/dist/esm/files/file-extensions.enum.js.map +1 -0
- package/dist/esm/files/mime-registry.d.ts +72 -0
- package/dist/esm/files/mime-registry.d.ts.map +1 -0
- package/dist/esm/files/mime-registry.js +174 -0
- package/dist/esm/files/mime-registry.js.map +1 -0
- package/dist/esm/files/unified-registry.d.ts +112 -0
- package/dist/esm/files/unified-registry.d.ts.map +1 -0
- package/dist/esm/files/unified-registry.js +190 -0
- package/dist/esm/files/unified-registry.js.map +1 -0
- package/dist/esm/index.d.ts +29 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +36 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/testing/decorated-test-schemas.d.ts +27 -0
- package/dist/esm/testing/decorated-test-schemas.d.ts.map +1 -0
- package/dist/esm/testing/decorated-test-schemas.js +26 -0
- package/dist/esm/testing/decorated-test-schemas.js.map +1 -0
- package/dist/esm/testing.d.ts +6 -0
- package/dist/esm/testing.d.ts.map +1 -0
- package/dist/esm/testing.js +6 -0
- package/dist/esm/testing.js.map +1 -0
- package/dist/esm/zod-bigdecimal.d.ts +193 -0
- package/dist/esm/zod-bigdecimal.d.ts.map +1 -0
- package/dist/esm/zod-bigdecimal.js +274 -0
- package/dist/esm/zod-bigdecimal.js.map +1 -0
- package/dist/esm/zod-code.d.ts +95 -0
- package/dist/esm/zod-code.d.ts.map +1 -0
- package/dist/esm/zod-code.js +160 -0
- package/dist/esm/zod-code.js.map +1 -0
- package/dist/esm/zod-color.d.ts +8 -0
- package/dist/esm/zod-color.d.ts.map +1 -0
- package/dist/esm/zod-color.js +88 -0
- package/dist/esm/zod-color.js.map +1 -0
- package/dist/esm/zod-decorators.types.d.ts +2207 -0
- package/dist/esm/zod-decorators.types.d.ts.map +1 -0
- package/dist/esm/zod-decorators.types.js +771 -0
- package/dist/esm/zod-decorators.types.js.map +1 -0
- package/dist/esm/zod-duration.d.ts +128 -0
- package/dist/esm/zod-duration.d.ts.map +1 -0
- package/dist/esm/zod-duration.js +174 -0
- package/dist/esm/zod-duration.js.map +1 -0
- package/dist/esm/zod-file.d.ts +219 -0
- package/dist/esm/zod-file.d.ts.map +1 -0
- package/dist/esm/zod-file.js +469 -0
- package/dist/esm/zod-file.js.map +1 -0
- package/dist/esm/zod-money.d.ts +223 -0
- package/dist/esm/zod-money.d.ts.map +1 -0
- package/dist/esm/zod-money.js +293 -0
- package/dist/esm/zod-money.js.map +1 -0
- package/dist/esm/zod-phone.d.ts +75 -0
- package/dist/esm/zod-phone.d.ts.map +1 -0
- package/dist/esm/zod-phone.js +116 -0
- package/dist/esm/zod-phone.js.map +1 -0
- package/dist/esm/zod-range.d.ts +87 -0
- package/dist/esm/zod-range.d.ts.map +1 -0
- package/dist/esm/zod-range.js +138 -0
- package/dist/esm/zod-range.js.map +1 -0
- package/dist/esm/zod-utils.shared.d.ts +783 -0
- package/dist/esm/zod-utils.shared.d.ts.map +1 -0
- package/dist/esm/zod-utils.shared.js +2647 -0
- package/dist/esm/zod-utils.shared.js.map +1 -0
- package/dist/esm/zod-versioned.d.ts +150 -0
- package/dist/esm/zod-versioned.d.ts.map +1 -0
- package/dist/esm/zod-versioned.js +172 -0
- package/dist/esm/zod-versioned.js.map +1 -0
- package/dist/esm/zod.module-override.d.ts +413 -0
- package/dist/esm/zod.module-override.d.ts.map +1 -0
- package/dist/esm/zod.module-override.js +1093 -0
- package/dist/esm/zod.module-override.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { DecoratorInput, BigDecimalSchemaOptions, ZodDecorator, ZodDecorator_ParamsType } from "./zod-decorators.types.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import Big from "big.js";
|
|
4
|
+
/**
|
|
5
|
+
* BigDecimal value type - represents a precise decimal number as a string.
|
|
6
|
+
*
|
|
7
|
+
* Using strings to store decimal values avoids JavaScript's floating-point
|
|
8
|
+
* precision issues. The big.js library is used for precise arithmetic operations.
|
|
9
|
+
*
|
|
10
|
+
* Example: "19.99", "0.00000001", "123456789.123456789"
|
|
11
|
+
*/
|
|
12
|
+
export type BigDecimalValue = string;
|
|
13
|
+
/**
|
|
14
|
+
* Zod schema for BigDecimalValue.
|
|
15
|
+
* Validates that the string represents a valid decimal number.
|
|
16
|
+
*/
|
|
17
|
+
export declare const BigDecimalValueSchema: z.ZodString;
|
|
18
|
+
/**
|
|
19
|
+
* z_bigdecimal() - Creates bigdecimal schemas for precise decimal numbers.
|
|
20
|
+
*
|
|
21
|
+
* BigDecimal values are stored as strings to avoid floating-point precision issues.
|
|
22
|
+
* The big.js library is used for arithmetic operations.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { z_bigdecimal } from '@wildo-ai/zod-decorators';
|
|
27
|
+
*
|
|
28
|
+
* const price = z_bigdecimal();
|
|
29
|
+
* const quantity = z_bigdecimal({ precision: 8 });
|
|
30
|
+
* const percentage = z_bigdecimal({ min: '0', max: '100' });
|
|
31
|
+
*
|
|
32
|
+
* // With decorators (chainable)
|
|
33
|
+
* z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })
|
|
34
|
+
* z_bigdecimal({ precision: 2 }).isHidden({ edit: true })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* Creates a bigdecimal schema with metadata for detection and form component rendering.
|
|
39
|
+
*
|
|
40
|
+
* @param options - Configuration options for bigdecimal field
|
|
41
|
+
* @returns A Zod string schema with metadata for bigdecimal detection
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* // Simple bigdecimal
|
|
46
|
+
* const price = z_bigdecimal();
|
|
47
|
+
* // Infers: string
|
|
48
|
+
*
|
|
49
|
+
* // With precision constraint
|
|
50
|
+
* const btcAmount = z_bigdecimal({ precision: 8 });
|
|
51
|
+
*
|
|
52
|
+
* // With min/max constraints
|
|
53
|
+
* const percentage = z_bigdecimal({
|
|
54
|
+
* min: '0',
|
|
55
|
+
* max: '100',
|
|
56
|
+
* precision: 2
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function createBigDecimalSchema(options?: BigDecimalSchemaOptions): DecoratedBigDecimalSchema;
|
|
61
|
+
/**
|
|
62
|
+
* Return type for bigdecimal schemas - includes decorator methods.
|
|
63
|
+
* Note: IS_FOREIGN_KEY is not applicable to bigdecimal values.
|
|
64
|
+
* IS_DB_INDEXED indexes the string value directly for query optimization.
|
|
65
|
+
*/
|
|
66
|
+
export type DecoratedBigDecimalSchema = z.ZodString & {
|
|
67
|
+
[ZodDecorator.IS_DB_INDEXED](): DecoratedBigDecimalSchema;
|
|
68
|
+
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedBigDecimalSchema;
|
|
69
|
+
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedBigDecimalSchema;
|
|
70
|
+
addDecorators(decorators: DecoratorInput[]): DecoratedBigDecimalSchema;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Creates a bigdecimal schema for precise decimal numbers stored as strings.
|
|
74
|
+
* Named z_bigdecimal to indicate it's a Zod helper function exported standalone.
|
|
75
|
+
*
|
|
76
|
+
* BigDecimal values are stored as strings to avoid floating-point precision issues.
|
|
77
|
+
* Use big.js for arithmetic operations on these values.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* import { z_bigdecimal } from '@wildo-ai/zod-decorators';
|
|
82
|
+
*
|
|
83
|
+
* // Simple usage
|
|
84
|
+
* z_bigdecimal() // Any valid decimal number as string
|
|
85
|
+
*
|
|
86
|
+
* // With options
|
|
87
|
+
* z_bigdecimal({ precision: 2 }) // Max 2 decimal places
|
|
88
|
+
* z_bigdecimal({ min: '0', max: '100' }) // Range constraint
|
|
89
|
+
* z_bigdecimal({ precision: 8, min: '0' }) // Crypto amount
|
|
90
|
+
*
|
|
91
|
+
* // With decorators (chainable)
|
|
92
|
+
* z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })
|
|
93
|
+
* z_bigdecimal({ precision: 2 }).isHidden({ edit: true })
|
|
94
|
+
*
|
|
95
|
+
* // Usage example
|
|
96
|
+
* const price: BigDecimalValue = "19.99";
|
|
97
|
+
* const btcAmount: BigDecimalValue = "0.00000001";
|
|
98
|
+
*
|
|
99
|
+
* // Arithmetic with big.js
|
|
100
|
+
* import Big from 'big.js';
|
|
101
|
+
* const total = new Big(price).plus("10.01").toString(); // "30.00"
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function z_bigdecimal(options?: BigDecimalSchemaOptions): DecoratedBigDecimalSchema;
|
|
105
|
+
/**
|
|
106
|
+
* Add two bigdecimal values.
|
|
107
|
+
* @param a - First value
|
|
108
|
+
* @param b - Second value
|
|
109
|
+
* @returns Sum as string
|
|
110
|
+
*/
|
|
111
|
+
export declare function bigDecimalAdd(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue;
|
|
112
|
+
/**
|
|
113
|
+
* Subtract two bigdecimal values.
|
|
114
|
+
* @param a - Value to subtract from
|
|
115
|
+
* @param b - Value to subtract
|
|
116
|
+
* @returns Difference as string
|
|
117
|
+
*/
|
|
118
|
+
export declare function bigDecimalSubtract(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue;
|
|
119
|
+
/**
|
|
120
|
+
* Multiply two bigdecimal values.
|
|
121
|
+
* @param a - First value
|
|
122
|
+
* @param b - Second value
|
|
123
|
+
* @returns Product as string
|
|
124
|
+
*/
|
|
125
|
+
export declare function bigDecimalMultiply(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue;
|
|
126
|
+
/**
|
|
127
|
+
* Divide two bigdecimal values.
|
|
128
|
+
* @param a - Dividend
|
|
129
|
+
* @param b - Divisor
|
|
130
|
+
* @param precision - Number of decimal places for result (default: 20)
|
|
131
|
+
* @returns Quotient as string
|
|
132
|
+
*/
|
|
133
|
+
export declare function bigDecimalDivide(a: BigDecimalValue, b: BigDecimalValue, precision?: number): BigDecimalValue;
|
|
134
|
+
/**
|
|
135
|
+
* Compare two bigdecimal values.
|
|
136
|
+
* @param a - First value
|
|
137
|
+
* @param b - Second value
|
|
138
|
+
* @returns -1 if a < b, 0 if a === b, 1 if a > b
|
|
139
|
+
*/
|
|
140
|
+
export declare function bigDecimalCompare(a: BigDecimalValue, b: BigDecimalValue): -1 | 0 | 1;
|
|
141
|
+
/**
|
|
142
|
+
* Round a bigdecimal value to specified precision.
|
|
143
|
+
* @param value - Value to round
|
|
144
|
+
* @param precision - Number of decimal places
|
|
145
|
+
* @param roundingMode - big.js rounding mode (default: ROUND_HALF_UP)
|
|
146
|
+
* @returns Rounded value as string
|
|
147
|
+
*/
|
|
148
|
+
export declare function bigDecimalRound(value: BigDecimalValue, precision: number, roundingMode?: Big.RoundingMode): BigDecimalValue;
|
|
149
|
+
/**
|
|
150
|
+
* Format a bigdecimal value for display with fixed decimal places.
|
|
151
|
+
* @param value - Value to format
|
|
152
|
+
* @param decimalPlaces - Number of decimal places to show
|
|
153
|
+
* @returns Formatted string with trailing zeros if needed
|
|
154
|
+
*/
|
|
155
|
+
export declare function bigDecimalFormat(value: BigDecimalValue, decimalPlaces: number): string;
|
|
156
|
+
/**
|
|
157
|
+
* Check if a bigdecimal value is zero.
|
|
158
|
+
* @param value - Value to check
|
|
159
|
+
* @returns true if value equals zero
|
|
160
|
+
*/
|
|
161
|
+
export declare function bigDecimalIsZero(value: BigDecimalValue): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Check if a bigdecimal value is positive (> 0).
|
|
164
|
+
* @param value - Value to check
|
|
165
|
+
* @returns true if value is greater than zero
|
|
166
|
+
*/
|
|
167
|
+
export declare function bigDecimalIsPositive(value: BigDecimalValue): boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Check if a bigdecimal value is negative (< 0).
|
|
170
|
+
* @param value - Value to check
|
|
171
|
+
* @returns true if value is less than zero
|
|
172
|
+
*/
|
|
173
|
+
export declare function bigDecimalIsNegative(value: BigDecimalValue): boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Get the absolute value of a bigdecimal.
|
|
176
|
+
* @param value - Value
|
|
177
|
+
* @returns Absolute value as string
|
|
178
|
+
*/
|
|
179
|
+
export declare function bigDecimalAbs(value: BigDecimalValue): BigDecimalValue;
|
|
180
|
+
/**
|
|
181
|
+
* Convert a number to bigdecimal string.
|
|
182
|
+
* @param value - Number to convert
|
|
183
|
+
* @returns String representation
|
|
184
|
+
*/
|
|
185
|
+
export declare function toBigDecimal(value: number): BigDecimalValue;
|
|
186
|
+
/**
|
|
187
|
+
* Convert a bigdecimal string to number.
|
|
188
|
+
* Warning: May lose precision for very large or very precise numbers.
|
|
189
|
+
* @param value - BigDecimal string
|
|
190
|
+
* @returns Number value
|
|
191
|
+
*/
|
|
192
|
+
export declare function fromBigDecimal(value: BigDecimalValue): number;
|
|
193
|
+
//# sourceMappingURL=zod-bigdecimal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-bigdecimal.d.ts","sourceRoot":"","sources":["../../src/zod-bigdecimal.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAkE,uBAAuB,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACxL,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,GAAG,MAAM,QAAQ,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAcrC;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,CAAC,CAAC,SAGtB,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,yBAAyB,CAkE3B;AAED;;;;GAIG;AACH,MAAM,MAAM,yBAAyB,GAAG,CAAC,CAAC,SAAS,GAAG;IACpD,CAAC,YAAY,CAAC,aAAa,CAAC,IAAI,yBAAyB,CAAC;IAC1D,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,EAAE,uBAAuB,CAAC,YAAY,CAAC,SAAS,CAAC,GAAG,yBAAyB,CAAC;IAC9G,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,EAAE,uBAAuB,CAAC,Y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;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,eAAe,GAAG,eAAe,CAErE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAE7D"}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// BIGDECIMAL SCHEMA HELPER
|
|
3
|
+
// ============================================================================
|
|
4
|
+
import { SCHEMA_METADATA, ZodDecorator } from "./zod-decorators.types.js";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { addChainableMethods } from "./zod.module-override.js";
|
|
7
|
+
import Big from "big.js";
|
|
8
|
+
/**
|
|
9
|
+
* Validates that a string represents a valid big decimal number.
|
|
10
|
+
*/
|
|
11
|
+
function isValidBigDecimal(value) {
|
|
12
|
+
try {
|
|
13
|
+
new Big(value);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Zod schema for BigDecimalValue.
|
|
22
|
+
* Validates that the string represents a valid decimal number.
|
|
23
|
+
*/
|
|
24
|
+
export const BigDecimalValueSchema = z.string().refine(isValidBigDecimal, { message: "Invalid decimal number format" });
|
|
25
|
+
/**
|
|
26
|
+
* z_bigdecimal() - Creates bigdecimal schemas for precise decimal numbers.
|
|
27
|
+
*
|
|
28
|
+
* BigDecimal values are stored as strings to avoid floating-point precision issues.
|
|
29
|
+
* The big.js library is used for arithmetic operations.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { z_bigdecimal } from '@wildo-ai/zod-decorators';
|
|
34
|
+
*
|
|
35
|
+
* const price = z_bigdecimal();
|
|
36
|
+
* const quantity = z_bigdecimal({ precision: 8 });
|
|
37
|
+
* const percentage = z_bigdecimal({ min: '0', max: '100' });
|
|
38
|
+
*
|
|
39
|
+
* // With decorators (chainable)
|
|
40
|
+
* z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })
|
|
41
|
+
* z_bigdecimal({ precision: 2 }).isHidden({ edit: true })
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Creates a bigdecimal schema with metadata for detection and form component rendering.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Configuration options for bigdecimal field
|
|
48
|
+
* @returns A Zod string schema with metadata for bigdecimal detection
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // Simple bigdecimal
|
|
53
|
+
* const price = z_bigdecimal();
|
|
54
|
+
* // Infers: string
|
|
55
|
+
*
|
|
56
|
+
* // With precision constraint
|
|
57
|
+
* const btcAmount = z_bigdecimal({ precision: 8 });
|
|
58
|
+
*
|
|
59
|
+
* // With min/max constraints
|
|
60
|
+
* const percentage = z_bigdecimal({
|
|
61
|
+
* min: '0',
|
|
62
|
+
* max: '100',
|
|
63
|
+
* precision: 2
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export function createBigDecimalSchema(options = {}) {
|
|
68
|
+
const { precision, min, max, } = options;
|
|
69
|
+
// Create the base schema with validation
|
|
70
|
+
let bigDecimalSchema = z.string().refine((value) => {
|
|
71
|
+
if (!isValidBigDecimal(value))
|
|
72
|
+
return false;
|
|
73
|
+
try {
|
|
74
|
+
const bigValue = new Big(value);
|
|
75
|
+
// Check precision (decimal places)
|
|
76
|
+
if (precision !== undefined) {
|
|
77
|
+
const decimalPlaces = value.includes('.')
|
|
78
|
+
? value.split('.')[1]?.length ?? 0
|
|
79
|
+
: 0;
|
|
80
|
+
if (decimalPlaces > precision)
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// Check min
|
|
84
|
+
if (min !== undefined) {
|
|
85
|
+
const minValue = new Big(min);
|
|
86
|
+
if (bigValue.lt(minValue))
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
// Check max
|
|
90
|
+
if (max !== undefined) {
|
|
91
|
+
const maxValue = new Big(max);
|
|
92
|
+
if (bigValue.gt(maxValue))
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
}, {
|
|
101
|
+
message: precision !== undefined
|
|
102
|
+
? `Invalid decimal number (max ${precision} decimal places${min !== undefined ? `, min: ${min}` : ''}${max !== undefined ? `, max: ${max}` : ''})`
|
|
103
|
+
: `Invalid decimal number${min !== undefined ? ` (min: ${min}` : ''}${max !== undefined ? `${min !== undefined ? ', ' : ' ('}max: ${max})` : min !== undefined ? ')' : ''}`
|
|
104
|
+
});
|
|
105
|
+
const bigDecimalMetadata = {
|
|
106
|
+
isBigDecimal: true,
|
|
107
|
+
...(precision !== undefined && { precision }),
|
|
108
|
+
...(min !== undefined && { min }),
|
|
109
|
+
...(max !== undefined && { max }),
|
|
110
|
+
};
|
|
111
|
+
// Initialize or extend SCHEMA_METADATA with bigdecimal config
|
|
112
|
+
const existingMetadata = bigDecimalSchema[SCHEMA_METADATA] || {};
|
|
113
|
+
bigDecimalSchema[SCHEMA_METADATA] = {
|
|
114
|
+
...existingMetadata,
|
|
115
|
+
bigDecimalConfig: bigDecimalMetadata,
|
|
116
|
+
};
|
|
117
|
+
// Add chainable decorator methods to the bigdecimal schema instance
|
|
118
|
+
// This allows: z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } }).isHidden()
|
|
119
|
+
addChainableMethods(bigDecimalSchema);
|
|
120
|
+
return bigDecimalSchema;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Creates a bigdecimal schema for precise decimal numbers stored as strings.
|
|
124
|
+
* Named z_bigdecimal to indicate it's a Zod helper function exported standalone.
|
|
125
|
+
*
|
|
126
|
+
* BigDecimal values are stored as strings to avoid floating-point precision issues.
|
|
127
|
+
* Use big.js for arithmetic operations on these values.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { z_bigdecimal } from '@wildo-ai/zod-decorators';
|
|
132
|
+
*
|
|
133
|
+
* // Simple usage
|
|
134
|
+
* z_bigdecimal() // Any valid decimal number as string
|
|
135
|
+
*
|
|
136
|
+
* // With options
|
|
137
|
+
* z_bigdecimal({ precision: 2 }) // Max 2 decimal places
|
|
138
|
+
* z_bigdecimal({ min: '0', max: '100' }) // Range constraint
|
|
139
|
+
* z_bigdecimal({ precision: 8, min: '0' }) // Crypto amount
|
|
140
|
+
*
|
|
141
|
+
* // With decorators (chainable)
|
|
142
|
+
* z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })
|
|
143
|
+
* z_bigdecimal({ precision: 2 }).isHidden({ edit: true })
|
|
144
|
+
*
|
|
145
|
+
* // Usage example
|
|
146
|
+
* const price: BigDecimalValue = "19.99";
|
|
147
|
+
* const btcAmount: BigDecimalValue = "0.00000001";
|
|
148
|
+
*
|
|
149
|
+
* // Arithmetic with big.js
|
|
150
|
+
* import Big from 'big.js';
|
|
151
|
+
* const total = new Big(price).plus("10.01").toString(); // "30.00"
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export function z_bigdecimal(options) {
|
|
155
|
+
return createBigDecimalSchema(options);
|
|
156
|
+
}
|
|
157
|
+
// ============================================================================
|
|
158
|
+
// BIGDECIMAL UTILITY FUNCTIONS
|
|
159
|
+
// ============================================================================
|
|
160
|
+
/**
|
|
161
|
+
* Add two bigdecimal values.
|
|
162
|
+
* @param a - First value
|
|
163
|
+
* @param b - Second value
|
|
164
|
+
* @returns Sum as string
|
|
165
|
+
*/
|
|
166
|
+
export function bigDecimalAdd(a, b) {
|
|
167
|
+
return new Big(a).plus(b).toString();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Subtract two bigdecimal values.
|
|
171
|
+
* @param a - Value to subtract from
|
|
172
|
+
* @param b - Value to subtract
|
|
173
|
+
* @returns Difference as string
|
|
174
|
+
*/
|
|
175
|
+
export function bigDecimalSubtract(a, b) {
|
|
176
|
+
return new Big(a).minus(b).toString();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Multiply two bigdecimal values.
|
|
180
|
+
* @param a - First value
|
|
181
|
+
* @param b - Second value
|
|
182
|
+
* @returns Product as string
|
|
183
|
+
*/
|
|
184
|
+
export function bigDecimalMultiply(a, b) {
|
|
185
|
+
return new Big(a).times(b).toString();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Divide two bigdecimal values.
|
|
189
|
+
* @param a - Dividend
|
|
190
|
+
* @param b - Divisor
|
|
191
|
+
* @param precision - Number of decimal places for result (default: 20)
|
|
192
|
+
* @returns Quotient as string
|
|
193
|
+
*/
|
|
194
|
+
export function bigDecimalDivide(a, b, precision = 20) {
|
|
195
|
+
return new Big(a).div(b).round(precision).toString();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Compare two bigdecimal values.
|
|
199
|
+
* @param a - First value
|
|
200
|
+
* @param b - Second value
|
|
201
|
+
* @returns -1 if a < b, 0 if a === b, 1 if a > b
|
|
202
|
+
*/
|
|
203
|
+
export function bigDecimalCompare(a, b) {
|
|
204
|
+
return new Big(a).cmp(b);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Round a bigdecimal value to specified precision.
|
|
208
|
+
* @param value - Value to round
|
|
209
|
+
* @param precision - Number of decimal places
|
|
210
|
+
* @param roundingMode - big.js rounding mode (default: ROUND_HALF_UP)
|
|
211
|
+
* @returns Rounded value as string
|
|
212
|
+
*/
|
|
213
|
+
export function bigDecimalRound(value, precision, roundingMode = Big.roundHalfUp) {
|
|
214
|
+
return new Big(value).round(precision, roundingMode).toString();
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Format a bigdecimal value for display with fixed decimal places.
|
|
218
|
+
* @param value - Value to format
|
|
219
|
+
* @param decimalPlaces - Number of decimal places to show
|
|
220
|
+
* @returns Formatted string with trailing zeros if needed
|
|
221
|
+
*/
|
|
222
|
+
export function bigDecimalFormat(value, decimalPlaces) {
|
|
223
|
+
return new Big(value).toFixed(decimalPlaces);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Check if a bigdecimal value is zero.
|
|
227
|
+
* @param value - Value to check
|
|
228
|
+
* @returns true if value equals zero
|
|
229
|
+
*/
|
|
230
|
+
export function bigDecimalIsZero(value) {
|
|
231
|
+
return new Big(value).eq(0);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Check if a bigdecimal value is positive (> 0).
|
|
235
|
+
* @param value - Value to check
|
|
236
|
+
* @returns true if value is greater than zero
|
|
237
|
+
*/
|
|
238
|
+
export function bigDecimalIsPositive(value) {
|
|
239
|
+
return new Big(value).gt(0);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Check if a bigdecimal value is negative (< 0).
|
|
243
|
+
* @param value - Value to check
|
|
244
|
+
* @returns true if value is less than zero
|
|
245
|
+
*/
|
|
246
|
+
export function bigDecimalIsNegative(value) {
|
|
247
|
+
return new Big(value).lt(0);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Get the absolute value of a bigdecimal.
|
|
251
|
+
* @param value - Value
|
|
252
|
+
* @returns Absolute value as string
|
|
253
|
+
*/
|
|
254
|
+
export function bigDecimalAbs(value) {
|
|
255
|
+
return new Big(value).abs().toString();
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Convert a number to bigdecimal string.
|
|
259
|
+
* @param value - Number to convert
|
|
260
|
+
* @returns String representation
|
|
261
|
+
*/
|
|
262
|
+
export function toBigDecimal(value) {
|
|
263
|
+
return new Big(value).toString();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Convert a bigdecimal string to number.
|
|
267
|
+
* Warning: May lose precision for very large or very precise numbers.
|
|
268
|
+
* @param value - BigDecimal string
|
|
269
|
+
* @returns Number value
|
|
270
|
+
*/
|
|
271
|
+
export function fromBigDecimal(value) {
|
|
272
|
+
return new Big(value).toNumber();
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=zod-bigdecimal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-bigdecimal.js","sourceRoot":"","sources":["../../src/zod-bigdecimal.ts"],"names":[],"mappings":"AACA,+EAA+E;AAC/E,2BAA2B;AAC3B,+EAA+E;AAE/E,OAAO,EAAkB,eAAe,EAA0E,YAAY,EAA2B,MAAM,wBAAwB,CAAC;AACxL,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,GAAG,MAAM,QAAQ,CAAC;AAYzB;;GAEG;AACH,SAAS,iBAAiB,CAAC,KAAa;IACtC,IAAI,CAAC;QACH,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;QACf,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAgB,CAAC,CAAC,MAAM,EAAE,CAAC,MAAM,CACjE,iBAAiB,EACjB,EAAE,OAAO,EAAE,+BAA+B,EAAE,CAC9B,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,sBAAsB,CACpC,UAAmC,EAAE;IAErC,MAAM,EACJ,SAAS,EACT,GAAG,EACH,GAAG,GACJ,GAAG,OAAO,CAAC;IAEZ,yCAAyC;IACzC,IAAI,gBAAgB,G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AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAsB;IACrD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAsB;IACzD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAsB;IACzD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAsB;IAClD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB;IACnD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { DecoratorInput, CodeSchemaOptions, CodeLanguage, ZodDecorator, ZodDecorator_ParamsType } from "./zod-decorators.types.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
/**
|
|
4
|
+
* Code value type - represents a code snippet with language information.
|
|
5
|
+
*/
|
|
6
|
+
export interface CodeValue {
|
|
7
|
+
/** The code content */
|
|
8
|
+
content: string;
|
|
9
|
+
/** Programming language for syntax highlighting and validation */
|
|
10
|
+
language: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Common programming languages for code schema language field.
|
|
14
|
+
* Can be extended with any string value.
|
|
15
|
+
*/
|
|
16
|
+
export declare const CommonCodeLanguages: readonly ["javascript", "typescript", "python", "java", "csharp", "cpp", "c", "go", "rust", "ruby", "php", "swift", "kotlin", "scala", "html", "css", "scss", "less", "json", "yaml", "xml", "markdown", "sql", "graphql", "bash", "shell", "powershell", "dockerfile", "nginx", "apache", "plaintext"];
|
|
17
|
+
/**
|
|
18
|
+
* Zod schema for CodeValue.
|
|
19
|
+
*/
|
|
20
|
+
export declare const CodeValueSchema: z.ZodObject<{
|
|
21
|
+
content: z.ZodString;
|
|
22
|
+
language: z.ZodString;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* z_code() - Creates code schemas for code snippets with language.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { z_code } from '@wildo-ai/zod-decorators';
|
|
30
|
+
*
|
|
31
|
+
* const snippet = z_code();
|
|
32
|
+
* const restrictedCode = z_code({
|
|
33
|
+
* allowedLanguages: ['javascript', 'typescript', 'python'],
|
|
34
|
+
* maxLength: 10000
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // UI props are now set via .codeUI():
|
|
38
|
+
* z_code().codeUI({ defaultLanguage: 'javascript', syntaxHighlighting: true })
|
|
39
|
+
* z_code().codeUI({ display: { showLineNumbers: true }, edit: { showLineNumbers: false } })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Creates a code schema with metadata for detection and form component rendering.
|
|
44
|
+
*
|
|
45
|
+
* @param options - Configuration options for code field
|
|
46
|
+
* @returns A Zod object schema with metadata for code detection
|
|
47
|
+
*/
|
|
48
|
+
export declare function createCodeSchema(options?: CodeSchemaOptions): DecoratedCodeSchema;
|
|
49
|
+
/**
|
|
50
|
+
* Return type for code schemas - includes decorator methods.
|
|
51
|
+
* IS_DB_INDEXED will index internal fields (content, language) for query optimization.
|
|
52
|
+
*/
|
|
53
|
+
export type DecoratedCodeSchema = z.ZodObject<{
|
|
54
|
+
content: z.ZodString;
|
|
55
|
+
language: z.ZodString;
|
|
56
|
+
}> & {
|
|
57
|
+
[ZodDecorator.IS_DB_INDEXED](): DecoratedCodeSchema;
|
|
58
|
+
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedCodeSchema;
|
|
59
|
+
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedCodeSchema;
|
|
60
|
+
addDecorators(decorators: DecoratorInput[]): DecoratedCodeSchema;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Creates a code schema for code snippets with language information.
|
|
64
|
+
* Named z_code to indicate it's a Zod helper function exported standalone.
|
|
65
|
+
*
|
|
66
|
+
* Code values are stored as: { content: string, language: string }
|
|
67
|
+
* - content: The actual code text
|
|
68
|
+
* - language: Programming language identifier for syntax highlighting
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { z_code } from '@wildo-ai/zod-decorators';
|
|
73
|
+
*
|
|
74
|
+
* // Simple usage
|
|
75
|
+
* z_code() // All languages, default 'plaintext'
|
|
76
|
+
*
|
|
77
|
+
* // With options (structural only)
|
|
78
|
+
* z_code({ allowedLanguages: ['javascript', 'typescript'] })
|
|
79
|
+
* z_code({ maxLength: 5000 })
|
|
80
|
+
*
|
|
81
|
+
* // UI props via .codeUI()
|
|
82
|
+
* z_code().codeUI({ defaultLanguage: 'typescript' })
|
|
83
|
+
* z_code().codeUI({ display: { showLineNumbers: false } })
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function z_code(options?: CodeSchemaOptions): DecoratedCodeSchema;
|
|
87
|
+
/**
|
|
88
|
+
* Get the display name for a code language.
|
|
89
|
+
*/
|
|
90
|
+
export declare function getLanguageDisplayName(language: CodeLanguage): string;
|
|
91
|
+
/**
|
|
92
|
+
* Check if a language is in the common languages list.
|
|
93
|
+
*/
|
|
94
|
+
export declare function isCommonLanguage(language: string): language is typeof CommonCodeLanguages[number];
|
|
95
|
+
//# sourceMappingURL=zod-code.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-code.d.ts","sourceRoot":"","sources":["../../src/zod-code.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAA4D,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAC1L,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,ySAOtB,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,CAAC,CAAC,SAAS,CAAC;IAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;IAAC,QAAQ,EAAE,CAAC,CAAC,SAAS,CAAA;CAAE,CAGvF,CAAC;AAEH;;;;;;;;;;;;;;;;;GAiBG;AAEH;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,iBAAsB,GAC9B,mBAAmB,CA6CrB;AAED;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,SAAS,CAAC;IAC5C,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;IACrB,QAAQ,EAAE,CAAC,CAAC,SAAS,CAAC;CACvB,CAAC,GAAG;IACH,CAAC,YAAY,CAAC,aAAa,CAAC,IAAI,mBAAmB,CAAC;IACpD,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,EAAE,uBAAuB,CAAC,YAAY,CAAC,SAAS,CAAC,GAAG,mBAAmB,CAAC;IACxG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