@adaptic/utils 0.0.985 → 0.0.987
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/index.cjs +131 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +131 -34
- package/dist/index.mjs.map +1 -1
- package/dist/test.js +4863 -0
- package/dist/test.js.map +1 -1
- package/dist/types/trading-policy/defaults/default-trading-policy.d.ts +16 -3
- package/dist/types/trading-policy/defaults/default-trading-policy.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/effective-policy.schema.d.ts +24 -0
- package/dist/types/trading-policy/schemas/effective-policy.schema.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/execution-prefs.schema.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/position-management-prefs.schema.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/risk-budget-prefs.schema.d.ts +10 -2
- package/dist/types/trading-policy/schemas/risk-budget-prefs.schema.d.ts.map +1 -1
- package/dist/types/trading-policy/schemas/signal-consumption-prefs.schema.d.ts +20 -4
- package/dist/types/trading-policy/schemas/signal-consumption-prefs.schema.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/test.js
CHANGED
|
@@ -3050,9 +3050,4870 @@ function formatNumber(value) {
|
|
|
3050
3050
|
return new Intl.NumberFormat("en-US").format(value);
|
|
3051
3051
|
}
|
|
3052
3052
|
|
|
3053
|
+
/**
|
|
3054
|
+
* Mirror enums for the trading policy preference system.
|
|
3055
|
+
* These enums are used by both the trading engine and the frontend app
|
|
3056
|
+
* for shared validation and type safety across packages.
|
|
3057
|
+
*/
|
|
3058
|
+
/** Controls the level of automation for trade execution decisions. */
|
|
3059
|
+
var AutonomyMode;
|
|
3060
|
+
(function (AutonomyMode) {
|
|
3061
|
+
AutonomyMode["ADVISORY_ONLY"] = "ADVISORY_ONLY";
|
|
3062
|
+
AutonomyMode["EXECUTION_ON_APPROVAL"] = "EXECUTION_ON_APPROVAL";
|
|
3063
|
+
AutonomyMode["SEMI_AUTONOMOUS"] = "SEMI_AUTONOMOUS";
|
|
3064
|
+
AutonomyMode["FULLY_AUTONOMOUS"] = "FULLY_AUTONOMOUS";
|
|
3065
|
+
AutonomyMode["EMERGENCY_SAFE_MODE"] = "EMERGENCY_SAFE_MODE";
|
|
3066
|
+
})(AutonomyMode || (AutonomyMode = {}));
|
|
3067
|
+
/** Categorizes protective overlay triggers that modify trading behavior. */
|
|
3068
|
+
var OverlayType;
|
|
3069
|
+
(function (OverlayType) {
|
|
3070
|
+
OverlayType["BLACK_SWAN"] = "BLACK_SWAN";
|
|
3071
|
+
OverlayType["VOLATILITY_REGIME"] = "VOLATILITY_REGIME";
|
|
3072
|
+
OverlayType["SECTOR_DETERIORATION"] = "SECTOR_DETERIORATION";
|
|
3073
|
+
OverlayType["DRAWDOWN_BREACH"] = "DRAWDOWN_BREACH";
|
|
3074
|
+
OverlayType["CORRELATION_SPIKE"] = "CORRELATION_SPIKE";
|
|
3075
|
+
OverlayType["LIQUIDITY_STRESS"] = "LIQUIDITY_STRESS";
|
|
3076
|
+
OverlayType["EXCHANGE_DEGRADATION"] = "EXCHANGE_DEGRADATION";
|
|
3077
|
+
OverlayType["DATA_QUALITY"] = "DATA_QUALITY";
|
|
3078
|
+
OverlayType["NEWS_EVENT_RISK"] = "NEWS_EVENT_RISK";
|
|
3079
|
+
OverlayType["RATES_BONDS_STRESS"] = "RATES_BONDS_STRESS";
|
|
3080
|
+
OverlayType["MANUAL_OVERRIDE"] = "MANUAL_OVERRIDE";
|
|
3081
|
+
OverlayType["INCIDENT_RESPONSE"] = "INCIDENT_RESPONSE";
|
|
3082
|
+
})(OverlayType || (OverlayType = {}));
|
|
3083
|
+
/** Severity level for an active overlay. */
|
|
3084
|
+
var OverlaySeverity;
|
|
3085
|
+
(function (OverlaySeverity) {
|
|
3086
|
+
OverlaySeverity["LOW"] = "LOW";
|
|
3087
|
+
OverlaySeverity["MEDIUM"] = "MEDIUM";
|
|
3088
|
+
OverlaySeverity["HIGH"] = "HIGH";
|
|
3089
|
+
OverlaySeverity["CRITICAL"] = "CRITICAL";
|
|
3090
|
+
})(OverlaySeverity || (OverlaySeverity = {}));
|
|
3091
|
+
/** Lifecycle status of an overlay instance. */
|
|
3092
|
+
var OverlayStatus;
|
|
3093
|
+
(function (OverlayStatus) {
|
|
3094
|
+
OverlayStatus["ACTIVE"] = "ACTIVE";
|
|
3095
|
+
OverlayStatus["EXPIRED"] = "EXPIRED";
|
|
3096
|
+
OverlayStatus["DEACTIVATED"] = "DEACTIVATED";
|
|
3097
|
+
OverlayStatus["SUPERSEDED"] = "SUPERSEDED";
|
|
3098
|
+
})(OverlayStatus || (OverlayStatus = {}));
|
|
3099
|
+
/** The action outcome chosen by the decision engine for a given signal. */
|
|
3100
|
+
var DecisionOutcome;
|
|
3101
|
+
(function (DecisionOutcome) {
|
|
3102
|
+
DecisionOutcome["DO_NOTHING"] = "DO_NOTHING";
|
|
3103
|
+
DecisionOutcome["OPEN_POSITION"] = "OPEN_POSITION";
|
|
3104
|
+
DecisionOutcome["ADD_TO_POSITION"] = "ADD_TO_POSITION";
|
|
3105
|
+
DecisionOutcome["REDUCE_POSITION"] = "REDUCE_POSITION";
|
|
3106
|
+
DecisionOutcome["CLOSE_POSITION"] = "CLOSE_POSITION";
|
|
3107
|
+
DecisionOutcome["REVERSE_POSITION"] = "REVERSE_POSITION";
|
|
3108
|
+
DecisionOutcome["MODIFY_ORDERS"] = "MODIFY_ORDERS";
|
|
3109
|
+
DecisionOutcome["CANCEL_ORDERS"] = "CANCEL_ORDERS";
|
|
3110
|
+
DecisionOutcome["REBALANCE"] = "REBALANCE";
|
|
3111
|
+
DecisionOutcome["MUTATE_POLICY"] = "MUTATE_POLICY";
|
|
3112
|
+
DecisionOutcome["ESCALATE_FOR_APPROVAL"] = "ESCALATE_FOR_APPROVAL";
|
|
3113
|
+
DecisionOutcome["SKIP_INELIGIBLE"] = "SKIP_INELIGIBLE";
|
|
3114
|
+
})(DecisionOutcome || (DecisionOutcome = {}));
|
|
3115
|
+
/** Tracks the execution lifecycle of a decision record. */
|
|
3116
|
+
var DecisionRecordStatus;
|
|
3117
|
+
(function (DecisionRecordStatus) {
|
|
3118
|
+
DecisionRecordStatus["PENDING"] = "PENDING";
|
|
3119
|
+
DecisionRecordStatus["EXECUTING"] = "EXECUTING";
|
|
3120
|
+
DecisionRecordStatus["COMPLETED"] = "COMPLETED";
|
|
3121
|
+
DecisionRecordStatus["FAILED"] = "FAILED";
|
|
3122
|
+
DecisionRecordStatus["CANCELLED"] = "CANCELLED";
|
|
3123
|
+
DecisionRecordStatus["ESCALATED"] = "ESCALATED";
|
|
3124
|
+
})(DecisionRecordStatus || (DecisionRecordStatus = {}));
|
|
3125
|
+
/** Tracks the P&L outcome of a decision for memory/learning purposes. */
|
|
3126
|
+
var DecisionMemoryOutcome;
|
|
3127
|
+
(function (DecisionMemoryOutcome) {
|
|
3128
|
+
DecisionMemoryOutcome["PENDING"] = "PENDING";
|
|
3129
|
+
DecisionMemoryOutcome["PROFITABLE"] = "PROFITABLE";
|
|
3130
|
+
DecisionMemoryOutcome["UNPROFITABLE"] = "UNPROFITABLE";
|
|
3131
|
+
DecisionMemoryOutcome["STOPPED_OUT"] = "STOPPED_OUT";
|
|
3132
|
+
DecisionMemoryOutcome["CANCELLED"] = "CANCELLED";
|
|
3133
|
+
})(DecisionMemoryOutcome || (DecisionMemoryOutcome = {}));
|
|
3134
|
+
/** Supported LLM providers for model-based decision making. */
|
|
3135
|
+
var LlmProvider;
|
|
3136
|
+
(function (LlmProvider) {
|
|
3137
|
+
LlmProvider["OPENAI"] = "OPENAI";
|
|
3138
|
+
LlmProvider["ANTHROPIC"] = "ANTHROPIC";
|
|
3139
|
+
LlmProvider["DEEPSEEK"] = "DEEPSEEK";
|
|
3140
|
+
LlmProvider["KIMI"] = "KIMI";
|
|
3141
|
+
LlmProvider["QWEN"] = "QWEN";
|
|
3142
|
+
LlmProvider["XAI"] = "XAI";
|
|
3143
|
+
LlmProvider["GEMINI"] = "GEMINI";
|
|
3144
|
+
})(LlmProvider || (LlmProvider = {}));
|
|
3145
|
+
|
|
3146
|
+
var util;
|
|
3147
|
+
(function (util) {
|
|
3148
|
+
util.assertEqual = (_) => { };
|
|
3149
|
+
function assertIs(_arg) { }
|
|
3150
|
+
util.assertIs = assertIs;
|
|
3151
|
+
function assertNever(_x) {
|
|
3152
|
+
throw new Error();
|
|
3153
|
+
}
|
|
3154
|
+
util.assertNever = assertNever;
|
|
3155
|
+
util.arrayToEnum = (items) => {
|
|
3156
|
+
const obj = {};
|
|
3157
|
+
for (const item of items) {
|
|
3158
|
+
obj[item] = item;
|
|
3159
|
+
}
|
|
3160
|
+
return obj;
|
|
3161
|
+
};
|
|
3162
|
+
util.getValidEnumValues = (obj) => {
|
|
3163
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
3164
|
+
const filtered = {};
|
|
3165
|
+
for (const k of validKeys) {
|
|
3166
|
+
filtered[k] = obj[k];
|
|
3167
|
+
}
|
|
3168
|
+
return util.objectValues(filtered);
|
|
3169
|
+
};
|
|
3170
|
+
util.objectValues = (obj) => {
|
|
3171
|
+
return util.objectKeys(obj).map(function (e) {
|
|
3172
|
+
return obj[e];
|
|
3173
|
+
});
|
|
3174
|
+
};
|
|
3175
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
3176
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
3177
|
+
: (object) => {
|
|
3178
|
+
const keys = [];
|
|
3179
|
+
for (const key in object) {
|
|
3180
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
3181
|
+
keys.push(key);
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
return keys;
|
|
3185
|
+
};
|
|
3186
|
+
util.find = (arr, checker) => {
|
|
3187
|
+
for (const item of arr) {
|
|
3188
|
+
if (checker(item))
|
|
3189
|
+
return item;
|
|
3190
|
+
}
|
|
3191
|
+
return undefined;
|
|
3192
|
+
};
|
|
3193
|
+
util.isInteger = typeof Number.isInteger === "function"
|
|
3194
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
3195
|
+
: (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
3196
|
+
function joinValues(array, separator = " | ") {
|
|
3197
|
+
return array.map((val) => (typeof val === "string" ? `'${val}'` : val)).join(separator);
|
|
3198
|
+
}
|
|
3199
|
+
util.joinValues = joinValues;
|
|
3200
|
+
util.jsonStringifyReplacer = (_, value) => {
|
|
3201
|
+
if (typeof value === "bigint") {
|
|
3202
|
+
return value.toString();
|
|
3203
|
+
}
|
|
3204
|
+
return value;
|
|
3205
|
+
};
|
|
3206
|
+
})(util || (util = {}));
|
|
3207
|
+
var objectUtil;
|
|
3208
|
+
(function (objectUtil) {
|
|
3209
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
3210
|
+
return {
|
|
3211
|
+
...first,
|
|
3212
|
+
...second, // second overwrites first
|
|
3213
|
+
};
|
|
3214
|
+
};
|
|
3215
|
+
})(objectUtil || (objectUtil = {}));
|
|
3216
|
+
const ZodParsedType = util.arrayToEnum([
|
|
3217
|
+
"string",
|
|
3218
|
+
"nan",
|
|
3219
|
+
"number",
|
|
3220
|
+
"integer",
|
|
3221
|
+
"float",
|
|
3222
|
+
"boolean",
|
|
3223
|
+
"date",
|
|
3224
|
+
"bigint",
|
|
3225
|
+
"symbol",
|
|
3226
|
+
"function",
|
|
3227
|
+
"undefined",
|
|
3228
|
+
"null",
|
|
3229
|
+
"array",
|
|
3230
|
+
"object",
|
|
3231
|
+
"unknown",
|
|
3232
|
+
"promise",
|
|
3233
|
+
"void",
|
|
3234
|
+
"never",
|
|
3235
|
+
"map",
|
|
3236
|
+
"set",
|
|
3237
|
+
]);
|
|
3238
|
+
const getParsedType = (data) => {
|
|
3239
|
+
const t = typeof data;
|
|
3240
|
+
switch (t) {
|
|
3241
|
+
case "undefined":
|
|
3242
|
+
return ZodParsedType.undefined;
|
|
3243
|
+
case "string":
|
|
3244
|
+
return ZodParsedType.string;
|
|
3245
|
+
case "number":
|
|
3246
|
+
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
3247
|
+
case "boolean":
|
|
3248
|
+
return ZodParsedType.boolean;
|
|
3249
|
+
case "function":
|
|
3250
|
+
return ZodParsedType.function;
|
|
3251
|
+
case "bigint":
|
|
3252
|
+
return ZodParsedType.bigint;
|
|
3253
|
+
case "symbol":
|
|
3254
|
+
return ZodParsedType.symbol;
|
|
3255
|
+
case "object":
|
|
3256
|
+
if (Array.isArray(data)) {
|
|
3257
|
+
return ZodParsedType.array;
|
|
3258
|
+
}
|
|
3259
|
+
if (data === null) {
|
|
3260
|
+
return ZodParsedType.null;
|
|
3261
|
+
}
|
|
3262
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
3263
|
+
return ZodParsedType.promise;
|
|
3264
|
+
}
|
|
3265
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
3266
|
+
return ZodParsedType.map;
|
|
3267
|
+
}
|
|
3268
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
3269
|
+
return ZodParsedType.set;
|
|
3270
|
+
}
|
|
3271
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
3272
|
+
return ZodParsedType.date;
|
|
3273
|
+
}
|
|
3274
|
+
return ZodParsedType.object;
|
|
3275
|
+
default:
|
|
3276
|
+
return ZodParsedType.unknown;
|
|
3277
|
+
}
|
|
3278
|
+
};
|
|
3279
|
+
|
|
3280
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
3281
|
+
"invalid_type",
|
|
3282
|
+
"invalid_literal",
|
|
3283
|
+
"custom",
|
|
3284
|
+
"invalid_union",
|
|
3285
|
+
"invalid_union_discriminator",
|
|
3286
|
+
"invalid_enum_value",
|
|
3287
|
+
"unrecognized_keys",
|
|
3288
|
+
"invalid_arguments",
|
|
3289
|
+
"invalid_return_type",
|
|
3290
|
+
"invalid_date",
|
|
3291
|
+
"invalid_string",
|
|
3292
|
+
"too_small",
|
|
3293
|
+
"too_big",
|
|
3294
|
+
"invalid_intersection_types",
|
|
3295
|
+
"not_multiple_of",
|
|
3296
|
+
"not_finite",
|
|
3297
|
+
]);
|
|
3298
|
+
class ZodError extends Error {
|
|
3299
|
+
get errors() {
|
|
3300
|
+
return this.issues;
|
|
3301
|
+
}
|
|
3302
|
+
constructor(issues) {
|
|
3303
|
+
super();
|
|
3304
|
+
this.issues = [];
|
|
3305
|
+
this.addIssue = (sub) => {
|
|
3306
|
+
this.issues = [...this.issues, sub];
|
|
3307
|
+
};
|
|
3308
|
+
this.addIssues = (subs = []) => {
|
|
3309
|
+
this.issues = [...this.issues, ...subs];
|
|
3310
|
+
};
|
|
3311
|
+
const actualProto = new.target.prototype;
|
|
3312
|
+
if (Object.setPrototypeOf) {
|
|
3313
|
+
// eslint-disable-next-line ban/ban
|
|
3314
|
+
Object.setPrototypeOf(this, actualProto);
|
|
3315
|
+
}
|
|
3316
|
+
else {
|
|
3317
|
+
this.__proto__ = actualProto;
|
|
3318
|
+
}
|
|
3319
|
+
this.name = "ZodError";
|
|
3320
|
+
this.issues = issues;
|
|
3321
|
+
}
|
|
3322
|
+
format(_mapper) {
|
|
3323
|
+
const mapper = _mapper ||
|
|
3324
|
+
function (issue) {
|
|
3325
|
+
return issue.message;
|
|
3326
|
+
};
|
|
3327
|
+
const fieldErrors = { _errors: [] };
|
|
3328
|
+
const processError = (error) => {
|
|
3329
|
+
for (const issue of error.issues) {
|
|
3330
|
+
if (issue.code === "invalid_union") {
|
|
3331
|
+
issue.unionErrors.map(processError);
|
|
3332
|
+
}
|
|
3333
|
+
else if (issue.code === "invalid_return_type") {
|
|
3334
|
+
processError(issue.returnTypeError);
|
|
3335
|
+
}
|
|
3336
|
+
else if (issue.code === "invalid_arguments") {
|
|
3337
|
+
processError(issue.argumentsError);
|
|
3338
|
+
}
|
|
3339
|
+
else if (issue.path.length === 0) {
|
|
3340
|
+
fieldErrors._errors.push(mapper(issue));
|
|
3341
|
+
}
|
|
3342
|
+
else {
|
|
3343
|
+
let curr = fieldErrors;
|
|
3344
|
+
let i = 0;
|
|
3345
|
+
while (i < issue.path.length) {
|
|
3346
|
+
const el = issue.path[i];
|
|
3347
|
+
const terminal = i === issue.path.length - 1;
|
|
3348
|
+
if (!terminal) {
|
|
3349
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
3350
|
+
// if (typeof el === "string") {
|
|
3351
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
3352
|
+
// } else if (typeof el === "number") {
|
|
3353
|
+
// const errorArray: any = [];
|
|
3354
|
+
// errorArray._errors = [];
|
|
3355
|
+
// curr[el] = curr[el] || errorArray;
|
|
3356
|
+
// }
|
|
3357
|
+
}
|
|
3358
|
+
else {
|
|
3359
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
3360
|
+
curr[el]._errors.push(mapper(issue));
|
|
3361
|
+
}
|
|
3362
|
+
curr = curr[el];
|
|
3363
|
+
i++;
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
};
|
|
3368
|
+
processError(this);
|
|
3369
|
+
return fieldErrors;
|
|
3370
|
+
}
|
|
3371
|
+
static assert(value) {
|
|
3372
|
+
if (!(value instanceof ZodError)) {
|
|
3373
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
toString() {
|
|
3377
|
+
return this.message;
|
|
3378
|
+
}
|
|
3379
|
+
get message() {
|
|
3380
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
3381
|
+
}
|
|
3382
|
+
get isEmpty() {
|
|
3383
|
+
return this.issues.length === 0;
|
|
3384
|
+
}
|
|
3385
|
+
flatten(mapper = (issue) => issue.message) {
|
|
3386
|
+
const fieldErrors = {};
|
|
3387
|
+
const formErrors = [];
|
|
3388
|
+
for (const sub of this.issues) {
|
|
3389
|
+
if (sub.path.length > 0) {
|
|
3390
|
+
const firstEl = sub.path[0];
|
|
3391
|
+
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
3392
|
+
fieldErrors[firstEl].push(mapper(sub));
|
|
3393
|
+
}
|
|
3394
|
+
else {
|
|
3395
|
+
formErrors.push(mapper(sub));
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
return { formErrors, fieldErrors };
|
|
3399
|
+
}
|
|
3400
|
+
get formErrors() {
|
|
3401
|
+
return this.flatten();
|
|
3402
|
+
}
|
|
3403
|
+
}
|
|
3404
|
+
ZodError.create = (issues) => {
|
|
3405
|
+
const error = new ZodError(issues);
|
|
3406
|
+
return error;
|
|
3407
|
+
};
|
|
3408
|
+
|
|
3409
|
+
const errorMap = (issue, _ctx) => {
|
|
3410
|
+
let message;
|
|
3411
|
+
switch (issue.code) {
|
|
3412
|
+
case ZodIssueCode.invalid_type:
|
|
3413
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
3414
|
+
message = "Required";
|
|
3415
|
+
}
|
|
3416
|
+
else {
|
|
3417
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
3418
|
+
}
|
|
3419
|
+
break;
|
|
3420
|
+
case ZodIssueCode.invalid_literal:
|
|
3421
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
3422
|
+
break;
|
|
3423
|
+
case ZodIssueCode.unrecognized_keys:
|
|
3424
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
3425
|
+
break;
|
|
3426
|
+
case ZodIssueCode.invalid_union:
|
|
3427
|
+
message = `Invalid input`;
|
|
3428
|
+
break;
|
|
3429
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
3430
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
3431
|
+
break;
|
|
3432
|
+
case ZodIssueCode.invalid_enum_value:
|
|
3433
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
3434
|
+
break;
|
|
3435
|
+
case ZodIssueCode.invalid_arguments:
|
|
3436
|
+
message = `Invalid function arguments`;
|
|
3437
|
+
break;
|
|
3438
|
+
case ZodIssueCode.invalid_return_type:
|
|
3439
|
+
message = `Invalid function return type`;
|
|
3440
|
+
break;
|
|
3441
|
+
case ZodIssueCode.invalid_date:
|
|
3442
|
+
message = `Invalid date`;
|
|
3443
|
+
break;
|
|
3444
|
+
case ZodIssueCode.invalid_string:
|
|
3445
|
+
if (typeof issue.validation === "object") {
|
|
3446
|
+
if ("includes" in issue.validation) {
|
|
3447
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
3448
|
+
if (typeof issue.validation.position === "number") {
|
|
3449
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
else if ("startsWith" in issue.validation) {
|
|
3453
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
3454
|
+
}
|
|
3455
|
+
else if ("endsWith" in issue.validation) {
|
|
3456
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
3457
|
+
}
|
|
3458
|
+
else {
|
|
3459
|
+
util.assertNever(issue.validation);
|
|
3460
|
+
}
|
|
3461
|
+
}
|
|
3462
|
+
else if (issue.validation !== "regex") {
|
|
3463
|
+
message = `Invalid ${issue.validation}`;
|
|
3464
|
+
}
|
|
3465
|
+
else {
|
|
3466
|
+
message = "Invalid";
|
|
3467
|
+
}
|
|
3468
|
+
break;
|
|
3469
|
+
case ZodIssueCode.too_small:
|
|
3470
|
+
if (issue.type === "array")
|
|
3471
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
3472
|
+
else if (issue.type === "string")
|
|
3473
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
3474
|
+
else if (issue.type === "number")
|
|
3475
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
3476
|
+
else if (issue.type === "bigint")
|
|
3477
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
3478
|
+
else if (issue.type === "date")
|
|
3479
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
3480
|
+
else
|
|
3481
|
+
message = "Invalid input";
|
|
3482
|
+
break;
|
|
3483
|
+
case ZodIssueCode.too_big:
|
|
3484
|
+
if (issue.type === "array")
|
|
3485
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
3486
|
+
else if (issue.type === "string")
|
|
3487
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
3488
|
+
else if (issue.type === "number")
|
|
3489
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
3490
|
+
else if (issue.type === "bigint")
|
|
3491
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
3492
|
+
else if (issue.type === "date")
|
|
3493
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
3494
|
+
else
|
|
3495
|
+
message = "Invalid input";
|
|
3496
|
+
break;
|
|
3497
|
+
case ZodIssueCode.custom:
|
|
3498
|
+
message = `Invalid input`;
|
|
3499
|
+
break;
|
|
3500
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
3501
|
+
message = `Intersection results could not be merged`;
|
|
3502
|
+
break;
|
|
3503
|
+
case ZodIssueCode.not_multiple_of:
|
|
3504
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
3505
|
+
break;
|
|
3506
|
+
case ZodIssueCode.not_finite:
|
|
3507
|
+
message = "Number must be finite";
|
|
3508
|
+
break;
|
|
3509
|
+
default:
|
|
3510
|
+
message = _ctx.defaultError;
|
|
3511
|
+
util.assertNever(issue);
|
|
3512
|
+
}
|
|
3513
|
+
return { message };
|
|
3514
|
+
};
|
|
3515
|
+
|
|
3516
|
+
let overrideErrorMap = errorMap;
|
|
3517
|
+
function getErrorMap() {
|
|
3518
|
+
return overrideErrorMap;
|
|
3519
|
+
}
|
|
3520
|
+
|
|
3521
|
+
const makeIssue = (params) => {
|
|
3522
|
+
const { data, path, errorMaps, issueData } = params;
|
|
3523
|
+
const fullPath = [...path, ...(issueData.path || [])];
|
|
3524
|
+
const fullIssue = {
|
|
3525
|
+
...issueData,
|
|
3526
|
+
path: fullPath,
|
|
3527
|
+
};
|
|
3528
|
+
if (issueData.message !== undefined) {
|
|
3529
|
+
return {
|
|
3530
|
+
...issueData,
|
|
3531
|
+
path: fullPath,
|
|
3532
|
+
message: issueData.message,
|
|
3533
|
+
};
|
|
3534
|
+
}
|
|
3535
|
+
let errorMessage = "";
|
|
3536
|
+
const maps = errorMaps
|
|
3537
|
+
.filter((m) => !!m)
|
|
3538
|
+
.slice()
|
|
3539
|
+
.reverse();
|
|
3540
|
+
for (const map of maps) {
|
|
3541
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
3542
|
+
}
|
|
3543
|
+
return {
|
|
3544
|
+
...issueData,
|
|
3545
|
+
path: fullPath,
|
|
3546
|
+
message: errorMessage,
|
|
3547
|
+
};
|
|
3548
|
+
};
|
|
3549
|
+
function addIssueToContext(ctx, issueData) {
|
|
3550
|
+
const overrideMap = getErrorMap();
|
|
3551
|
+
const issue = makeIssue({
|
|
3552
|
+
issueData: issueData,
|
|
3553
|
+
data: ctx.data,
|
|
3554
|
+
path: ctx.path,
|
|
3555
|
+
errorMaps: [
|
|
3556
|
+
ctx.common.contextualErrorMap, // contextual error map is first priority
|
|
3557
|
+
ctx.schemaErrorMap, // then schema-bound map if available
|
|
3558
|
+
overrideMap, // then global override map
|
|
3559
|
+
overrideMap === errorMap ? undefined : errorMap, // then global default map
|
|
3560
|
+
].filter((x) => !!x),
|
|
3561
|
+
});
|
|
3562
|
+
ctx.common.issues.push(issue);
|
|
3563
|
+
}
|
|
3564
|
+
class ParseStatus {
|
|
3565
|
+
constructor() {
|
|
3566
|
+
this.value = "valid";
|
|
3567
|
+
}
|
|
3568
|
+
dirty() {
|
|
3569
|
+
if (this.value === "valid")
|
|
3570
|
+
this.value = "dirty";
|
|
3571
|
+
}
|
|
3572
|
+
abort() {
|
|
3573
|
+
if (this.value !== "aborted")
|
|
3574
|
+
this.value = "aborted";
|
|
3575
|
+
}
|
|
3576
|
+
static mergeArray(status, results) {
|
|
3577
|
+
const arrayValue = [];
|
|
3578
|
+
for (const s of results) {
|
|
3579
|
+
if (s.status === "aborted")
|
|
3580
|
+
return INVALID;
|
|
3581
|
+
if (s.status === "dirty")
|
|
3582
|
+
status.dirty();
|
|
3583
|
+
arrayValue.push(s.value);
|
|
3584
|
+
}
|
|
3585
|
+
return { status: status.value, value: arrayValue };
|
|
3586
|
+
}
|
|
3587
|
+
static async mergeObjectAsync(status, pairs) {
|
|
3588
|
+
const syncPairs = [];
|
|
3589
|
+
for (const pair of pairs) {
|
|
3590
|
+
const key = await pair.key;
|
|
3591
|
+
const value = await pair.value;
|
|
3592
|
+
syncPairs.push({
|
|
3593
|
+
key,
|
|
3594
|
+
value,
|
|
3595
|
+
});
|
|
3596
|
+
}
|
|
3597
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
3598
|
+
}
|
|
3599
|
+
static mergeObjectSync(status, pairs) {
|
|
3600
|
+
const finalObject = {};
|
|
3601
|
+
for (const pair of pairs) {
|
|
3602
|
+
const { key, value } = pair;
|
|
3603
|
+
if (key.status === "aborted")
|
|
3604
|
+
return INVALID;
|
|
3605
|
+
if (value.status === "aborted")
|
|
3606
|
+
return INVALID;
|
|
3607
|
+
if (key.status === "dirty")
|
|
3608
|
+
status.dirty();
|
|
3609
|
+
if (value.status === "dirty")
|
|
3610
|
+
status.dirty();
|
|
3611
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
3612
|
+
finalObject[key.value] = value.value;
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
return { status: status.value, value: finalObject };
|
|
3616
|
+
}
|
|
3617
|
+
}
|
|
3618
|
+
const INVALID = Object.freeze({
|
|
3619
|
+
status: "aborted",
|
|
3620
|
+
});
|
|
3621
|
+
const DIRTY = (value) => ({ status: "dirty", value });
|
|
3622
|
+
const OK = (value) => ({ status: "valid", value });
|
|
3623
|
+
const isAborted = (x) => x.status === "aborted";
|
|
3624
|
+
const isDirty = (x) => x.status === "dirty";
|
|
3625
|
+
const isValid = (x) => x.status === "valid";
|
|
3626
|
+
const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
3627
|
+
|
|
3628
|
+
var errorUtil;
|
|
3629
|
+
(function (errorUtil) {
|
|
3630
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
3631
|
+
// biome-ignore lint:
|
|
3632
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
3633
|
+
})(errorUtil || (errorUtil = {}));
|
|
3634
|
+
|
|
3635
|
+
class ParseInputLazyPath {
|
|
3636
|
+
constructor(parent, value, path, key) {
|
|
3637
|
+
this._cachedPath = [];
|
|
3638
|
+
this.parent = parent;
|
|
3639
|
+
this.data = value;
|
|
3640
|
+
this._path = path;
|
|
3641
|
+
this._key = key;
|
|
3642
|
+
}
|
|
3643
|
+
get path() {
|
|
3644
|
+
if (!this._cachedPath.length) {
|
|
3645
|
+
if (Array.isArray(this._key)) {
|
|
3646
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
3647
|
+
}
|
|
3648
|
+
else {
|
|
3649
|
+
this._cachedPath.push(...this._path, this._key);
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
return this._cachedPath;
|
|
3653
|
+
}
|
|
3654
|
+
}
|
|
3655
|
+
const handleResult = (ctx, result) => {
|
|
3656
|
+
if (isValid(result)) {
|
|
3657
|
+
return { success: true, data: result.value };
|
|
3658
|
+
}
|
|
3659
|
+
else {
|
|
3660
|
+
if (!ctx.common.issues.length) {
|
|
3661
|
+
throw new Error("Validation failed but no issues detected.");
|
|
3662
|
+
}
|
|
3663
|
+
return {
|
|
3664
|
+
success: false,
|
|
3665
|
+
get error() {
|
|
3666
|
+
if (this._error)
|
|
3667
|
+
return this._error;
|
|
3668
|
+
const error = new ZodError(ctx.common.issues);
|
|
3669
|
+
this._error = error;
|
|
3670
|
+
return this._error;
|
|
3671
|
+
},
|
|
3672
|
+
};
|
|
3673
|
+
}
|
|
3674
|
+
};
|
|
3675
|
+
function processCreateParams(params) {
|
|
3676
|
+
if (!params)
|
|
3677
|
+
return {};
|
|
3678
|
+
const { errorMap, invalid_type_error, required_error, description } = params;
|
|
3679
|
+
if (errorMap && (invalid_type_error || required_error)) {
|
|
3680
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
3681
|
+
}
|
|
3682
|
+
if (errorMap)
|
|
3683
|
+
return { errorMap: errorMap, description };
|
|
3684
|
+
const customMap = (iss, ctx) => {
|
|
3685
|
+
const { message } = params;
|
|
3686
|
+
if (iss.code === "invalid_enum_value") {
|
|
3687
|
+
return { message: message ?? ctx.defaultError };
|
|
3688
|
+
}
|
|
3689
|
+
if (typeof ctx.data === "undefined") {
|
|
3690
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
3691
|
+
}
|
|
3692
|
+
if (iss.code !== "invalid_type")
|
|
3693
|
+
return { message: ctx.defaultError };
|
|
3694
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
3695
|
+
};
|
|
3696
|
+
return { errorMap: customMap, description };
|
|
3697
|
+
}
|
|
3698
|
+
class ZodType {
|
|
3699
|
+
get description() {
|
|
3700
|
+
return this._def.description;
|
|
3701
|
+
}
|
|
3702
|
+
_getType(input) {
|
|
3703
|
+
return getParsedType(input.data);
|
|
3704
|
+
}
|
|
3705
|
+
_getOrReturnCtx(input, ctx) {
|
|
3706
|
+
return (ctx || {
|
|
3707
|
+
common: input.parent.common,
|
|
3708
|
+
data: input.data,
|
|
3709
|
+
parsedType: getParsedType(input.data),
|
|
3710
|
+
schemaErrorMap: this._def.errorMap,
|
|
3711
|
+
path: input.path,
|
|
3712
|
+
parent: input.parent,
|
|
3713
|
+
});
|
|
3714
|
+
}
|
|
3715
|
+
_processInputParams(input) {
|
|
3716
|
+
return {
|
|
3717
|
+
status: new ParseStatus(),
|
|
3718
|
+
ctx: {
|
|
3719
|
+
common: input.parent.common,
|
|
3720
|
+
data: input.data,
|
|
3721
|
+
parsedType: getParsedType(input.data),
|
|
3722
|
+
schemaErrorMap: this._def.errorMap,
|
|
3723
|
+
path: input.path,
|
|
3724
|
+
parent: input.parent,
|
|
3725
|
+
},
|
|
3726
|
+
};
|
|
3727
|
+
}
|
|
3728
|
+
_parseSync(input) {
|
|
3729
|
+
const result = this._parse(input);
|
|
3730
|
+
if (isAsync(result)) {
|
|
3731
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
3732
|
+
}
|
|
3733
|
+
return result;
|
|
3734
|
+
}
|
|
3735
|
+
_parseAsync(input) {
|
|
3736
|
+
const result = this._parse(input);
|
|
3737
|
+
return Promise.resolve(result);
|
|
3738
|
+
}
|
|
3739
|
+
parse(data, params) {
|
|
3740
|
+
const result = this.safeParse(data, params);
|
|
3741
|
+
if (result.success)
|
|
3742
|
+
return result.data;
|
|
3743
|
+
throw result.error;
|
|
3744
|
+
}
|
|
3745
|
+
safeParse(data, params) {
|
|
3746
|
+
const ctx = {
|
|
3747
|
+
common: {
|
|
3748
|
+
issues: [],
|
|
3749
|
+
async: params?.async ?? false,
|
|
3750
|
+
contextualErrorMap: params?.errorMap,
|
|
3751
|
+
},
|
|
3752
|
+
path: params?.path || [],
|
|
3753
|
+
schemaErrorMap: this._def.errorMap,
|
|
3754
|
+
parent: null,
|
|
3755
|
+
data,
|
|
3756
|
+
parsedType: getParsedType(data),
|
|
3757
|
+
};
|
|
3758
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
3759
|
+
return handleResult(ctx, result);
|
|
3760
|
+
}
|
|
3761
|
+
"~validate"(data) {
|
|
3762
|
+
const ctx = {
|
|
3763
|
+
common: {
|
|
3764
|
+
issues: [],
|
|
3765
|
+
async: !!this["~standard"].async,
|
|
3766
|
+
},
|
|
3767
|
+
path: [],
|
|
3768
|
+
schemaErrorMap: this._def.errorMap,
|
|
3769
|
+
parent: null,
|
|
3770
|
+
data,
|
|
3771
|
+
parsedType: getParsedType(data),
|
|
3772
|
+
};
|
|
3773
|
+
if (!this["~standard"].async) {
|
|
3774
|
+
try {
|
|
3775
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
3776
|
+
return isValid(result)
|
|
3777
|
+
? {
|
|
3778
|
+
value: result.value,
|
|
3779
|
+
}
|
|
3780
|
+
: {
|
|
3781
|
+
issues: ctx.common.issues,
|
|
3782
|
+
};
|
|
3783
|
+
}
|
|
3784
|
+
catch (err) {
|
|
3785
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
3786
|
+
this["~standard"].async = true;
|
|
3787
|
+
}
|
|
3788
|
+
ctx.common = {
|
|
3789
|
+
issues: [],
|
|
3790
|
+
async: true,
|
|
3791
|
+
};
|
|
3792
|
+
}
|
|
3793
|
+
}
|
|
3794
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result)
|
|
3795
|
+
? {
|
|
3796
|
+
value: result.value,
|
|
3797
|
+
}
|
|
3798
|
+
: {
|
|
3799
|
+
issues: ctx.common.issues,
|
|
3800
|
+
});
|
|
3801
|
+
}
|
|
3802
|
+
async parseAsync(data, params) {
|
|
3803
|
+
const result = await this.safeParseAsync(data, params);
|
|
3804
|
+
if (result.success)
|
|
3805
|
+
return result.data;
|
|
3806
|
+
throw result.error;
|
|
3807
|
+
}
|
|
3808
|
+
async safeParseAsync(data, params) {
|
|
3809
|
+
const ctx = {
|
|
3810
|
+
common: {
|
|
3811
|
+
issues: [],
|
|
3812
|
+
contextualErrorMap: params?.errorMap,
|
|
3813
|
+
async: true,
|
|
3814
|
+
},
|
|
3815
|
+
path: params?.path || [],
|
|
3816
|
+
schemaErrorMap: this._def.errorMap,
|
|
3817
|
+
parent: null,
|
|
3818
|
+
data,
|
|
3819
|
+
parsedType: getParsedType(data),
|
|
3820
|
+
};
|
|
3821
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
3822
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
3823
|
+
return handleResult(ctx, result);
|
|
3824
|
+
}
|
|
3825
|
+
refine(check, message) {
|
|
3826
|
+
const getIssueProperties = (val) => {
|
|
3827
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
3828
|
+
return { message };
|
|
3829
|
+
}
|
|
3830
|
+
else if (typeof message === "function") {
|
|
3831
|
+
return message(val);
|
|
3832
|
+
}
|
|
3833
|
+
else {
|
|
3834
|
+
return message;
|
|
3835
|
+
}
|
|
3836
|
+
};
|
|
3837
|
+
return this._refinement((val, ctx) => {
|
|
3838
|
+
const result = check(val);
|
|
3839
|
+
const setError = () => ctx.addIssue({
|
|
3840
|
+
code: ZodIssueCode.custom,
|
|
3841
|
+
...getIssueProperties(val),
|
|
3842
|
+
});
|
|
3843
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
3844
|
+
return result.then((data) => {
|
|
3845
|
+
if (!data) {
|
|
3846
|
+
setError();
|
|
3847
|
+
return false;
|
|
3848
|
+
}
|
|
3849
|
+
else {
|
|
3850
|
+
return true;
|
|
3851
|
+
}
|
|
3852
|
+
});
|
|
3853
|
+
}
|
|
3854
|
+
if (!result) {
|
|
3855
|
+
setError();
|
|
3856
|
+
return false;
|
|
3857
|
+
}
|
|
3858
|
+
else {
|
|
3859
|
+
return true;
|
|
3860
|
+
}
|
|
3861
|
+
});
|
|
3862
|
+
}
|
|
3863
|
+
refinement(check, refinementData) {
|
|
3864
|
+
return this._refinement((val, ctx) => {
|
|
3865
|
+
if (!check(val)) {
|
|
3866
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
3867
|
+
return false;
|
|
3868
|
+
}
|
|
3869
|
+
else {
|
|
3870
|
+
return true;
|
|
3871
|
+
}
|
|
3872
|
+
});
|
|
3873
|
+
}
|
|
3874
|
+
_refinement(refinement) {
|
|
3875
|
+
return new ZodEffects({
|
|
3876
|
+
schema: this,
|
|
3877
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3878
|
+
effect: { type: "refinement", refinement },
|
|
3879
|
+
});
|
|
3880
|
+
}
|
|
3881
|
+
superRefine(refinement) {
|
|
3882
|
+
return this._refinement(refinement);
|
|
3883
|
+
}
|
|
3884
|
+
constructor(def) {
|
|
3885
|
+
/** Alias of safeParseAsync */
|
|
3886
|
+
this.spa = this.safeParseAsync;
|
|
3887
|
+
this._def = def;
|
|
3888
|
+
this.parse = this.parse.bind(this);
|
|
3889
|
+
this.safeParse = this.safeParse.bind(this);
|
|
3890
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
3891
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
3892
|
+
this.spa = this.spa.bind(this);
|
|
3893
|
+
this.refine = this.refine.bind(this);
|
|
3894
|
+
this.refinement = this.refinement.bind(this);
|
|
3895
|
+
this.superRefine = this.superRefine.bind(this);
|
|
3896
|
+
this.optional = this.optional.bind(this);
|
|
3897
|
+
this.nullable = this.nullable.bind(this);
|
|
3898
|
+
this.nullish = this.nullish.bind(this);
|
|
3899
|
+
this.array = this.array.bind(this);
|
|
3900
|
+
this.promise = this.promise.bind(this);
|
|
3901
|
+
this.or = this.or.bind(this);
|
|
3902
|
+
this.and = this.and.bind(this);
|
|
3903
|
+
this.transform = this.transform.bind(this);
|
|
3904
|
+
this.brand = this.brand.bind(this);
|
|
3905
|
+
this.default = this.default.bind(this);
|
|
3906
|
+
this.catch = this.catch.bind(this);
|
|
3907
|
+
this.describe = this.describe.bind(this);
|
|
3908
|
+
this.pipe = this.pipe.bind(this);
|
|
3909
|
+
this.readonly = this.readonly.bind(this);
|
|
3910
|
+
this.isNullable = this.isNullable.bind(this);
|
|
3911
|
+
this.isOptional = this.isOptional.bind(this);
|
|
3912
|
+
this["~standard"] = {
|
|
3913
|
+
version: 1,
|
|
3914
|
+
vendor: "zod",
|
|
3915
|
+
validate: (data) => this["~validate"](data),
|
|
3916
|
+
};
|
|
3917
|
+
}
|
|
3918
|
+
optional() {
|
|
3919
|
+
return ZodOptional.create(this, this._def);
|
|
3920
|
+
}
|
|
3921
|
+
nullable() {
|
|
3922
|
+
return ZodNullable.create(this, this._def);
|
|
3923
|
+
}
|
|
3924
|
+
nullish() {
|
|
3925
|
+
return this.nullable().optional();
|
|
3926
|
+
}
|
|
3927
|
+
array() {
|
|
3928
|
+
return ZodArray.create(this);
|
|
3929
|
+
}
|
|
3930
|
+
promise() {
|
|
3931
|
+
return ZodPromise.create(this, this._def);
|
|
3932
|
+
}
|
|
3933
|
+
or(option) {
|
|
3934
|
+
return ZodUnion.create([this, option], this._def);
|
|
3935
|
+
}
|
|
3936
|
+
and(incoming) {
|
|
3937
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
3938
|
+
}
|
|
3939
|
+
transform(transform) {
|
|
3940
|
+
return new ZodEffects({
|
|
3941
|
+
...processCreateParams(this._def),
|
|
3942
|
+
schema: this,
|
|
3943
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3944
|
+
effect: { type: "transform", transform },
|
|
3945
|
+
});
|
|
3946
|
+
}
|
|
3947
|
+
default(def) {
|
|
3948
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
3949
|
+
return new ZodDefault({
|
|
3950
|
+
...processCreateParams(this._def),
|
|
3951
|
+
innerType: this,
|
|
3952
|
+
defaultValue: defaultValueFunc,
|
|
3953
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
3954
|
+
});
|
|
3955
|
+
}
|
|
3956
|
+
brand() {
|
|
3957
|
+
return new ZodBranded({
|
|
3958
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
3959
|
+
type: this,
|
|
3960
|
+
...processCreateParams(this._def),
|
|
3961
|
+
});
|
|
3962
|
+
}
|
|
3963
|
+
catch(def) {
|
|
3964
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
3965
|
+
return new ZodCatch({
|
|
3966
|
+
...processCreateParams(this._def),
|
|
3967
|
+
innerType: this,
|
|
3968
|
+
catchValue: catchValueFunc,
|
|
3969
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
3970
|
+
});
|
|
3971
|
+
}
|
|
3972
|
+
describe(description) {
|
|
3973
|
+
const This = this.constructor;
|
|
3974
|
+
return new This({
|
|
3975
|
+
...this._def,
|
|
3976
|
+
description,
|
|
3977
|
+
});
|
|
3978
|
+
}
|
|
3979
|
+
pipe(target) {
|
|
3980
|
+
return ZodPipeline.create(this, target);
|
|
3981
|
+
}
|
|
3982
|
+
readonly() {
|
|
3983
|
+
return ZodReadonly.create(this);
|
|
3984
|
+
}
|
|
3985
|
+
isOptional() {
|
|
3986
|
+
return this.safeParse(undefined).success;
|
|
3987
|
+
}
|
|
3988
|
+
isNullable() {
|
|
3989
|
+
return this.safeParse(null).success;
|
|
3990
|
+
}
|
|
3991
|
+
}
|
|
3992
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
3993
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
3994
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
3995
|
+
// const uuidRegex =
|
|
3996
|
+
// /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
3997
|
+
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
3998
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
3999
|
+
const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
4000
|
+
const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
4001
|
+
// from https://stackoverflow.com/a/46181/1550155
|
|
4002
|
+
// old version: too slow, didn't support unicode
|
|
4003
|
+
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
4004
|
+
//old email regex
|
|
4005
|
+
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
4006
|
+
// eslint-disable-next-line
|
|
4007
|
+
// const emailRegex =
|
|
4008
|
+
// /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
|
|
4009
|
+
// const emailRegex =
|
|
4010
|
+
// /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
4011
|
+
// const emailRegex =
|
|
4012
|
+
// /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
|
|
4013
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
4014
|
+
// const emailRegex =
|
|
4015
|
+
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
4016
|
+
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
4017
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
4018
|
+
let emojiRegex;
|
|
4019
|
+
// faster, simpler, safer
|
|
4020
|
+
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
4021
|
+
const ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
4022
|
+
// const ipv6Regex =
|
|
4023
|
+
// /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
|
|
4024
|
+
const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
4025
|
+
const ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
4026
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
4027
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
4028
|
+
// https://base64.guru/standards/base64url
|
|
4029
|
+
const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
4030
|
+
// simple
|
|
4031
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
4032
|
+
// no leap year validation
|
|
4033
|
+
// const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
|
|
4034
|
+
// with leap year validation
|
|
4035
|
+
const dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
4036
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
4037
|
+
function timeRegexSource(args) {
|
|
4038
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
4039
|
+
if (args.precision) {
|
|
4040
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
4041
|
+
}
|
|
4042
|
+
else if (args.precision == null) {
|
|
4043
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
4044
|
+
}
|
|
4045
|
+
const secondsQuantifier = args.precision ? "+" : "?"; // require seconds if precision is nonzero
|
|
4046
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
4047
|
+
}
|
|
4048
|
+
function timeRegex(args) {
|
|
4049
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
4050
|
+
}
|
|
4051
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
4052
|
+
function datetimeRegex(args) {
|
|
4053
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
4054
|
+
const opts = [];
|
|
4055
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
4056
|
+
if (args.offset)
|
|
4057
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
4058
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
4059
|
+
return new RegExp(`^${regex}$`);
|
|
4060
|
+
}
|
|
4061
|
+
function isValidIP(ip, version) {
|
|
4062
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
4063
|
+
return true;
|
|
4064
|
+
}
|
|
4065
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
4066
|
+
return true;
|
|
4067
|
+
}
|
|
4068
|
+
return false;
|
|
4069
|
+
}
|
|
4070
|
+
function isValidJWT(jwt, alg) {
|
|
4071
|
+
if (!jwtRegex.test(jwt))
|
|
4072
|
+
return false;
|
|
4073
|
+
try {
|
|
4074
|
+
const [header] = jwt.split(".");
|
|
4075
|
+
if (!header)
|
|
4076
|
+
return false;
|
|
4077
|
+
// Convert base64url to base64
|
|
4078
|
+
const base64 = header
|
|
4079
|
+
.replace(/-/g, "+")
|
|
4080
|
+
.replace(/_/g, "/")
|
|
4081
|
+
.padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
|
|
4082
|
+
const decoded = JSON.parse(atob(base64));
|
|
4083
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
4084
|
+
return false;
|
|
4085
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
4086
|
+
return false;
|
|
4087
|
+
if (!decoded.alg)
|
|
4088
|
+
return false;
|
|
4089
|
+
if (alg && decoded.alg !== alg)
|
|
4090
|
+
return false;
|
|
4091
|
+
return true;
|
|
4092
|
+
}
|
|
4093
|
+
catch {
|
|
4094
|
+
return false;
|
|
4095
|
+
}
|
|
4096
|
+
}
|
|
4097
|
+
function isValidCidr(ip, version) {
|
|
4098
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
4099
|
+
return true;
|
|
4100
|
+
}
|
|
4101
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
4102
|
+
return true;
|
|
4103
|
+
}
|
|
4104
|
+
return false;
|
|
4105
|
+
}
|
|
4106
|
+
class ZodString extends ZodType {
|
|
4107
|
+
_parse(input) {
|
|
4108
|
+
if (this._def.coerce) {
|
|
4109
|
+
input.data = String(input.data);
|
|
4110
|
+
}
|
|
4111
|
+
const parsedType = this._getType(input);
|
|
4112
|
+
if (parsedType !== ZodParsedType.string) {
|
|
4113
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4114
|
+
addIssueToContext(ctx, {
|
|
4115
|
+
code: ZodIssueCode.invalid_type,
|
|
4116
|
+
expected: ZodParsedType.string,
|
|
4117
|
+
received: ctx.parsedType,
|
|
4118
|
+
});
|
|
4119
|
+
return INVALID;
|
|
4120
|
+
}
|
|
4121
|
+
const status = new ParseStatus();
|
|
4122
|
+
let ctx = undefined;
|
|
4123
|
+
for (const check of this._def.checks) {
|
|
4124
|
+
if (check.kind === "min") {
|
|
4125
|
+
if (input.data.length < check.value) {
|
|
4126
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4127
|
+
addIssueToContext(ctx, {
|
|
4128
|
+
code: ZodIssueCode.too_small,
|
|
4129
|
+
minimum: check.value,
|
|
4130
|
+
type: "string",
|
|
4131
|
+
inclusive: true,
|
|
4132
|
+
exact: false,
|
|
4133
|
+
message: check.message,
|
|
4134
|
+
});
|
|
4135
|
+
status.dirty();
|
|
4136
|
+
}
|
|
4137
|
+
}
|
|
4138
|
+
else if (check.kind === "max") {
|
|
4139
|
+
if (input.data.length > check.value) {
|
|
4140
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4141
|
+
addIssueToContext(ctx, {
|
|
4142
|
+
code: ZodIssueCode.too_big,
|
|
4143
|
+
maximum: check.value,
|
|
4144
|
+
type: "string",
|
|
4145
|
+
inclusive: true,
|
|
4146
|
+
exact: false,
|
|
4147
|
+
message: check.message,
|
|
4148
|
+
});
|
|
4149
|
+
status.dirty();
|
|
4150
|
+
}
|
|
4151
|
+
}
|
|
4152
|
+
else if (check.kind === "length") {
|
|
4153
|
+
const tooBig = input.data.length > check.value;
|
|
4154
|
+
const tooSmall = input.data.length < check.value;
|
|
4155
|
+
if (tooBig || tooSmall) {
|
|
4156
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4157
|
+
if (tooBig) {
|
|
4158
|
+
addIssueToContext(ctx, {
|
|
4159
|
+
code: ZodIssueCode.too_big,
|
|
4160
|
+
maximum: check.value,
|
|
4161
|
+
type: "string",
|
|
4162
|
+
inclusive: true,
|
|
4163
|
+
exact: true,
|
|
4164
|
+
message: check.message,
|
|
4165
|
+
});
|
|
4166
|
+
}
|
|
4167
|
+
else if (tooSmall) {
|
|
4168
|
+
addIssueToContext(ctx, {
|
|
4169
|
+
code: ZodIssueCode.too_small,
|
|
4170
|
+
minimum: check.value,
|
|
4171
|
+
type: "string",
|
|
4172
|
+
inclusive: true,
|
|
4173
|
+
exact: true,
|
|
4174
|
+
message: check.message,
|
|
4175
|
+
});
|
|
4176
|
+
}
|
|
4177
|
+
status.dirty();
|
|
4178
|
+
}
|
|
4179
|
+
}
|
|
4180
|
+
else if (check.kind === "email") {
|
|
4181
|
+
if (!emailRegex.test(input.data)) {
|
|
4182
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4183
|
+
addIssueToContext(ctx, {
|
|
4184
|
+
validation: "email",
|
|
4185
|
+
code: ZodIssueCode.invalid_string,
|
|
4186
|
+
message: check.message,
|
|
4187
|
+
});
|
|
4188
|
+
status.dirty();
|
|
4189
|
+
}
|
|
4190
|
+
}
|
|
4191
|
+
else if (check.kind === "emoji") {
|
|
4192
|
+
if (!emojiRegex) {
|
|
4193
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
4194
|
+
}
|
|
4195
|
+
if (!emojiRegex.test(input.data)) {
|
|
4196
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4197
|
+
addIssueToContext(ctx, {
|
|
4198
|
+
validation: "emoji",
|
|
4199
|
+
code: ZodIssueCode.invalid_string,
|
|
4200
|
+
message: check.message,
|
|
4201
|
+
});
|
|
4202
|
+
status.dirty();
|
|
4203
|
+
}
|
|
4204
|
+
}
|
|
4205
|
+
else if (check.kind === "uuid") {
|
|
4206
|
+
if (!uuidRegex.test(input.data)) {
|
|
4207
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4208
|
+
addIssueToContext(ctx, {
|
|
4209
|
+
validation: "uuid",
|
|
4210
|
+
code: ZodIssueCode.invalid_string,
|
|
4211
|
+
message: check.message,
|
|
4212
|
+
});
|
|
4213
|
+
status.dirty();
|
|
4214
|
+
}
|
|
4215
|
+
}
|
|
4216
|
+
else if (check.kind === "nanoid") {
|
|
4217
|
+
if (!nanoidRegex.test(input.data)) {
|
|
4218
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4219
|
+
addIssueToContext(ctx, {
|
|
4220
|
+
validation: "nanoid",
|
|
4221
|
+
code: ZodIssueCode.invalid_string,
|
|
4222
|
+
message: check.message,
|
|
4223
|
+
});
|
|
4224
|
+
status.dirty();
|
|
4225
|
+
}
|
|
4226
|
+
}
|
|
4227
|
+
else if (check.kind === "cuid") {
|
|
4228
|
+
if (!cuidRegex.test(input.data)) {
|
|
4229
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4230
|
+
addIssueToContext(ctx, {
|
|
4231
|
+
validation: "cuid",
|
|
4232
|
+
code: ZodIssueCode.invalid_string,
|
|
4233
|
+
message: check.message,
|
|
4234
|
+
});
|
|
4235
|
+
status.dirty();
|
|
4236
|
+
}
|
|
4237
|
+
}
|
|
4238
|
+
else if (check.kind === "cuid2") {
|
|
4239
|
+
if (!cuid2Regex.test(input.data)) {
|
|
4240
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4241
|
+
addIssueToContext(ctx, {
|
|
4242
|
+
validation: "cuid2",
|
|
4243
|
+
code: ZodIssueCode.invalid_string,
|
|
4244
|
+
message: check.message,
|
|
4245
|
+
});
|
|
4246
|
+
status.dirty();
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
else if (check.kind === "ulid") {
|
|
4250
|
+
if (!ulidRegex.test(input.data)) {
|
|
4251
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4252
|
+
addIssueToContext(ctx, {
|
|
4253
|
+
validation: "ulid",
|
|
4254
|
+
code: ZodIssueCode.invalid_string,
|
|
4255
|
+
message: check.message,
|
|
4256
|
+
});
|
|
4257
|
+
status.dirty();
|
|
4258
|
+
}
|
|
4259
|
+
}
|
|
4260
|
+
else if (check.kind === "url") {
|
|
4261
|
+
try {
|
|
4262
|
+
new URL(input.data);
|
|
4263
|
+
}
|
|
4264
|
+
catch {
|
|
4265
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4266
|
+
addIssueToContext(ctx, {
|
|
4267
|
+
validation: "url",
|
|
4268
|
+
code: ZodIssueCode.invalid_string,
|
|
4269
|
+
message: check.message,
|
|
4270
|
+
});
|
|
4271
|
+
status.dirty();
|
|
4272
|
+
}
|
|
4273
|
+
}
|
|
4274
|
+
else if (check.kind === "regex") {
|
|
4275
|
+
check.regex.lastIndex = 0;
|
|
4276
|
+
const testResult = check.regex.test(input.data);
|
|
4277
|
+
if (!testResult) {
|
|
4278
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4279
|
+
addIssueToContext(ctx, {
|
|
4280
|
+
validation: "regex",
|
|
4281
|
+
code: ZodIssueCode.invalid_string,
|
|
4282
|
+
message: check.message,
|
|
4283
|
+
});
|
|
4284
|
+
status.dirty();
|
|
4285
|
+
}
|
|
4286
|
+
}
|
|
4287
|
+
else if (check.kind === "trim") {
|
|
4288
|
+
input.data = input.data.trim();
|
|
4289
|
+
}
|
|
4290
|
+
else if (check.kind === "includes") {
|
|
4291
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
4292
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4293
|
+
addIssueToContext(ctx, {
|
|
4294
|
+
code: ZodIssueCode.invalid_string,
|
|
4295
|
+
validation: { includes: check.value, position: check.position },
|
|
4296
|
+
message: check.message,
|
|
4297
|
+
});
|
|
4298
|
+
status.dirty();
|
|
4299
|
+
}
|
|
4300
|
+
}
|
|
4301
|
+
else if (check.kind === "toLowerCase") {
|
|
4302
|
+
input.data = input.data.toLowerCase();
|
|
4303
|
+
}
|
|
4304
|
+
else if (check.kind === "toUpperCase") {
|
|
4305
|
+
input.data = input.data.toUpperCase();
|
|
4306
|
+
}
|
|
4307
|
+
else if (check.kind === "startsWith") {
|
|
4308
|
+
if (!input.data.startsWith(check.value)) {
|
|
4309
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4310
|
+
addIssueToContext(ctx, {
|
|
4311
|
+
code: ZodIssueCode.invalid_string,
|
|
4312
|
+
validation: { startsWith: check.value },
|
|
4313
|
+
message: check.message,
|
|
4314
|
+
});
|
|
4315
|
+
status.dirty();
|
|
4316
|
+
}
|
|
4317
|
+
}
|
|
4318
|
+
else if (check.kind === "endsWith") {
|
|
4319
|
+
if (!input.data.endsWith(check.value)) {
|
|
4320
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4321
|
+
addIssueToContext(ctx, {
|
|
4322
|
+
code: ZodIssueCode.invalid_string,
|
|
4323
|
+
validation: { endsWith: check.value },
|
|
4324
|
+
message: check.message,
|
|
4325
|
+
});
|
|
4326
|
+
status.dirty();
|
|
4327
|
+
}
|
|
4328
|
+
}
|
|
4329
|
+
else if (check.kind === "datetime") {
|
|
4330
|
+
const regex = datetimeRegex(check);
|
|
4331
|
+
if (!regex.test(input.data)) {
|
|
4332
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4333
|
+
addIssueToContext(ctx, {
|
|
4334
|
+
code: ZodIssueCode.invalid_string,
|
|
4335
|
+
validation: "datetime",
|
|
4336
|
+
message: check.message,
|
|
4337
|
+
});
|
|
4338
|
+
status.dirty();
|
|
4339
|
+
}
|
|
4340
|
+
}
|
|
4341
|
+
else if (check.kind === "date") {
|
|
4342
|
+
const regex = dateRegex;
|
|
4343
|
+
if (!regex.test(input.data)) {
|
|
4344
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4345
|
+
addIssueToContext(ctx, {
|
|
4346
|
+
code: ZodIssueCode.invalid_string,
|
|
4347
|
+
validation: "date",
|
|
4348
|
+
message: check.message,
|
|
4349
|
+
});
|
|
4350
|
+
status.dirty();
|
|
4351
|
+
}
|
|
4352
|
+
}
|
|
4353
|
+
else if (check.kind === "time") {
|
|
4354
|
+
const regex = timeRegex(check);
|
|
4355
|
+
if (!regex.test(input.data)) {
|
|
4356
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4357
|
+
addIssueToContext(ctx, {
|
|
4358
|
+
code: ZodIssueCode.invalid_string,
|
|
4359
|
+
validation: "time",
|
|
4360
|
+
message: check.message,
|
|
4361
|
+
});
|
|
4362
|
+
status.dirty();
|
|
4363
|
+
}
|
|
4364
|
+
}
|
|
4365
|
+
else if (check.kind === "duration") {
|
|
4366
|
+
if (!durationRegex.test(input.data)) {
|
|
4367
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4368
|
+
addIssueToContext(ctx, {
|
|
4369
|
+
validation: "duration",
|
|
4370
|
+
code: ZodIssueCode.invalid_string,
|
|
4371
|
+
message: check.message,
|
|
4372
|
+
});
|
|
4373
|
+
status.dirty();
|
|
4374
|
+
}
|
|
4375
|
+
}
|
|
4376
|
+
else if (check.kind === "ip") {
|
|
4377
|
+
if (!isValidIP(input.data, check.version)) {
|
|
4378
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4379
|
+
addIssueToContext(ctx, {
|
|
4380
|
+
validation: "ip",
|
|
4381
|
+
code: ZodIssueCode.invalid_string,
|
|
4382
|
+
message: check.message,
|
|
4383
|
+
});
|
|
4384
|
+
status.dirty();
|
|
4385
|
+
}
|
|
4386
|
+
}
|
|
4387
|
+
else if (check.kind === "jwt") {
|
|
4388
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
4389
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4390
|
+
addIssueToContext(ctx, {
|
|
4391
|
+
validation: "jwt",
|
|
4392
|
+
code: ZodIssueCode.invalid_string,
|
|
4393
|
+
message: check.message,
|
|
4394
|
+
});
|
|
4395
|
+
status.dirty();
|
|
4396
|
+
}
|
|
4397
|
+
}
|
|
4398
|
+
else if (check.kind === "cidr") {
|
|
4399
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
4400
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4401
|
+
addIssueToContext(ctx, {
|
|
4402
|
+
validation: "cidr",
|
|
4403
|
+
code: ZodIssueCode.invalid_string,
|
|
4404
|
+
message: check.message,
|
|
4405
|
+
});
|
|
4406
|
+
status.dirty();
|
|
4407
|
+
}
|
|
4408
|
+
}
|
|
4409
|
+
else if (check.kind === "base64") {
|
|
4410
|
+
if (!base64Regex.test(input.data)) {
|
|
4411
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4412
|
+
addIssueToContext(ctx, {
|
|
4413
|
+
validation: "base64",
|
|
4414
|
+
code: ZodIssueCode.invalid_string,
|
|
4415
|
+
message: check.message,
|
|
4416
|
+
});
|
|
4417
|
+
status.dirty();
|
|
4418
|
+
}
|
|
4419
|
+
}
|
|
4420
|
+
else if (check.kind === "base64url") {
|
|
4421
|
+
if (!base64urlRegex.test(input.data)) {
|
|
4422
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4423
|
+
addIssueToContext(ctx, {
|
|
4424
|
+
validation: "base64url",
|
|
4425
|
+
code: ZodIssueCode.invalid_string,
|
|
4426
|
+
message: check.message,
|
|
4427
|
+
});
|
|
4428
|
+
status.dirty();
|
|
4429
|
+
}
|
|
4430
|
+
}
|
|
4431
|
+
else {
|
|
4432
|
+
util.assertNever(check);
|
|
4433
|
+
}
|
|
4434
|
+
}
|
|
4435
|
+
return { status: status.value, value: input.data };
|
|
4436
|
+
}
|
|
4437
|
+
_regex(regex, validation, message) {
|
|
4438
|
+
return this.refinement((data) => regex.test(data), {
|
|
4439
|
+
validation,
|
|
4440
|
+
code: ZodIssueCode.invalid_string,
|
|
4441
|
+
...errorUtil.errToObj(message),
|
|
4442
|
+
});
|
|
4443
|
+
}
|
|
4444
|
+
_addCheck(check) {
|
|
4445
|
+
return new ZodString({
|
|
4446
|
+
...this._def,
|
|
4447
|
+
checks: [...this._def.checks, check],
|
|
4448
|
+
});
|
|
4449
|
+
}
|
|
4450
|
+
email(message) {
|
|
4451
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
4452
|
+
}
|
|
4453
|
+
url(message) {
|
|
4454
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
4455
|
+
}
|
|
4456
|
+
emoji(message) {
|
|
4457
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
4458
|
+
}
|
|
4459
|
+
uuid(message) {
|
|
4460
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
4461
|
+
}
|
|
4462
|
+
nanoid(message) {
|
|
4463
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
4464
|
+
}
|
|
4465
|
+
cuid(message) {
|
|
4466
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
4467
|
+
}
|
|
4468
|
+
cuid2(message) {
|
|
4469
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
4470
|
+
}
|
|
4471
|
+
ulid(message) {
|
|
4472
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
4473
|
+
}
|
|
4474
|
+
base64(message) {
|
|
4475
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
4476
|
+
}
|
|
4477
|
+
base64url(message) {
|
|
4478
|
+
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
4479
|
+
return this._addCheck({
|
|
4480
|
+
kind: "base64url",
|
|
4481
|
+
...errorUtil.errToObj(message),
|
|
4482
|
+
});
|
|
4483
|
+
}
|
|
4484
|
+
jwt(options) {
|
|
4485
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
4486
|
+
}
|
|
4487
|
+
ip(options) {
|
|
4488
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
4489
|
+
}
|
|
4490
|
+
cidr(options) {
|
|
4491
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
4492
|
+
}
|
|
4493
|
+
datetime(options) {
|
|
4494
|
+
if (typeof options === "string") {
|
|
4495
|
+
return this._addCheck({
|
|
4496
|
+
kind: "datetime",
|
|
4497
|
+
precision: null,
|
|
4498
|
+
offset: false,
|
|
4499
|
+
local: false,
|
|
4500
|
+
message: options,
|
|
4501
|
+
});
|
|
4502
|
+
}
|
|
4503
|
+
return this._addCheck({
|
|
4504
|
+
kind: "datetime",
|
|
4505
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
4506
|
+
offset: options?.offset ?? false,
|
|
4507
|
+
local: options?.local ?? false,
|
|
4508
|
+
...errorUtil.errToObj(options?.message),
|
|
4509
|
+
});
|
|
4510
|
+
}
|
|
4511
|
+
date(message) {
|
|
4512
|
+
return this._addCheck({ kind: "date", message });
|
|
4513
|
+
}
|
|
4514
|
+
time(options) {
|
|
4515
|
+
if (typeof options === "string") {
|
|
4516
|
+
return this._addCheck({
|
|
4517
|
+
kind: "time",
|
|
4518
|
+
precision: null,
|
|
4519
|
+
message: options,
|
|
4520
|
+
});
|
|
4521
|
+
}
|
|
4522
|
+
return this._addCheck({
|
|
4523
|
+
kind: "time",
|
|
4524
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
4525
|
+
...errorUtil.errToObj(options?.message),
|
|
4526
|
+
});
|
|
4527
|
+
}
|
|
4528
|
+
duration(message) {
|
|
4529
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
4530
|
+
}
|
|
4531
|
+
regex(regex, message) {
|
|
4532
|
+
return this._addCheck({
|
|
4533
|
+
kind: "regex",
|
|
4534
|
+
regex: regex,
|
|
4535
|
+
...errorUtil.errToObj(message),
|
|
4536
|
+
});
|
|
4537
|
+
}
|
|
4538
|
+
includes(value, options) {
|
|
4539
|
+
return this._addCheck({
|
|
4540
|
+
kind: "includes",
|
|
4541
|
+
value: value,
|
|
4542
|
+
position: options?.position,
|
|
4543
|
+
...errorUtil.errToObj(options?.message),
|
|
4544
|
+
});
|
|
4545
|
+
}
|
|
4546
|
+
startsWith(value, message) {
|
|
4547
|
+
return this._addCheck({
|
|
4548
|
+
kind: "startsWith",
|
|
4549
|
+
value: value,
|
|
4550
|
+
...errorUtil.errToObj(message),
|
|
4551
|
+
});
|
|
4552
|
+
}
|
|
4553
|
+
endsWith(value, message) {
|
|
4554
|
+
return this._addCheck({
|
|
4555
|
+
kind: "endsWith",
|
|
4556
|
+
value: value,
|
|
4557
|
+
...errorUtil.errToObj(message),
|
|
4558
|
+
});
|
|
4559
|
+
}
|
|
4560
|
+
min(minLength, message) {
|
|
4561
|
+
return this._addCheck({
|
|
4562
|
+
kind: "min",
|
|
4563
|
+
value: minLength,
|
|
4564
|
+
...errorUtil.errToObj(message),
|
|
4565
|
+
});
|
|
4566
|
+
}
|
|
4567
|
+
max(maxLength, message) {
|
|
4568
|
+
return this._addCheck({
|
|
4569
|
+
kind: "max",
|
|
4570
|
+
value: maxLength,
|
|
4571
|
+
...errorUtil.errToObj(message),
|
|
4572
|
+
});
|
|
4573
|
+
}
|
|
4574
|
+
length(len, message) {
|
|
4575
|
+
return this._addCheck({
|
|
4576
|
+
kind: "length",
|
|
4577
|
+
value: len,
|
|
4578
|
+
...errorUtil.errToObj(message),
|
|
4579
|
+
});
|
|
4580
|
+
}
|
|
4581
|
+
/**
|
|
4582
|
+
* Equivalent to `.min(1)`
|
|
4583
|
+
*/
|
|
4584
|
+
nonempty(message) {
|
|
4585
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
4586
|
+
}
|
|
4587
|
+
trim() {
|
|
4588
|
+
return new ZodString({
|
|
4589
|
+
...this._def,
|
|
4590
|
+
checks: [...this._def.checks, { kind: "trim" }],
|
|
4591
|
+
});
|
|
4592
|
+
}
|
|
4593
|
+
toLowerCase() {
|
|
4594
|
+
return new ZodString({
|
|
4595
|
+
...this._def,
|
|
4596
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
4597
|
+
});
|
|
4598
|
+
}
|
|
4599
|
+
toUpperCase() {
|
|
4600
|
+
return new ZodString({
|
|
4601
|
+
...this._def,
|
|
4602
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
4603
|
+
});
|
|
4604
|
+
}
|
|
4605
|
+
get isDatetime() {
|
|
4606
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
4607
|
+
}
|
|
4608
|
+
get isDate() {
|
|
4609
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
4610
|
+
}
|
|
4611
|
+
get isTime() {
|
|
4612
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
4613
|
+
}
|
|
4614
|
+
get isDuration() {
|
|
4615
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
4616
|
+
}
|
|
4617
|
+
get isEmail() {
|
|
4618
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
4619
|
+
}
|
|
4620
|
+
get isURL() {
|
|
4621
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
4622
|
+
}
|
|
4623
|
+
get isEmoji() {
|
|
4624
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
4625
|
+
}
|
|
4626
|
+
get isUUID() {
|
|
4627
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
4628
|
+
}
|
|
4629
|
+
get isNANOID() {
|
|
4630
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
4631
|
+
}
|
|
4632
|
+
get isCUID() {
|
|
4633
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
4634
|
+
}
|
|
4635
|
+
get isCUID2() {
|
|
4636
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
4637
|
+
}
|
|
4638
|
+
get isULID() {
|
|
4639
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
4640
|
+
}
|
|
4641
|
+
get isIP() {
|
|
4642
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
4643
|
+
}
|
|
4644
|
+
get isCIDR() {
|
|
4645
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
4646
|
+
}
|
|
4647
|
+
get isBase64() {
|
|
4648
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
4649
|
+
}
|
|
4650
|
+
get isBase64url() {
|
|
4651
|
+
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
4652
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
4653
|
+
}
|
|
4654
|
+
get minLength() {
|
|
4655
|
+
let min = null;
|
|
4656
|
+
for (const ch of this._def.checks) {
|
|
4657
|
+
if (ch.kind === "min") {
|
|
4658
|
+
if (min === null || ch.value > min)
|
|
4659
|
+
min = ch.value;
|
|
4660
|
+
}
|
|
4661
|
+
}
|
|
4662
|
+
return min;
|
|
4663
|
+
}
|
|
4664
|
+
get maxLength() {
|
|
4665
|
+
let max = null;
|
|
4666
|
+
for (const ch of this._def.checks) {
|
|
4667
|
+
if (ch.kind === "max") {
|
|
4668
|
+
if (max === null || ch.value < max)
|
|
4669
|
+
max = ch.value;
|
|
4670
|
+
}
|
|
4671
|
+
}
|
|
4672
|
+
return max;
|
|
4673
|
+
}
|
|
4674
|
+
}
|
|
4675
|
+
ZodString.create = (params) => {
|
|
4676
|
+
return new ZodString({
|
|
4677
|
+
checks: [],
|
|
4678
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
4679
|
+
coerce: params?.coerce ?? false,
|
|
4680
|
+
...processCreateParams(params),
|
|
4681
|
+
});
|
|
4682
|
+
};
|
|
4683
|
+
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
4684
|
+
function floatSafeRemainder(val, step) {
|
|
4685
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
4686
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
4687
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
4688
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
4689
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
4690
|
+
return (valInt % stepInt) / 10 ** decCount;
|
|
4691
|
+
}
|
|
4692
|
+
class ZodNumber extends ZodType {
|
|
4693
|
+
constructor() {
|
|
4694
|
+
super(...arguments);
|
|
4695
|
+
this.min = this.gte;
|
|
4696
|
+
this.max = this.lte;
|
|
4697
|
+
this.step = this.multipleOf;
|
|
4698
|
+
}
|
|
4699
|
+
_parse(input) {
|
|
4700
|
+
if (this._def.coerce) {
|
|
4701
|
+
input.data = Number(input.data);
|
|
4702
|
+
}
|
|
4703
|
+
const parsedType = this._getType(input);
|
|
4704
|
+
if (parsedType !== ZodParsedType.number) {
|
|
4705
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4706
|
+
addIssueToContext(ctx, {
|
|
4707
|
+
code: ZodIssueCode.invalid_type,
|
|
4708
|
+
expected: ZodParsedType.number,
|
|
4709
|
+
received: ctx.parsedType,
|
|
4710
|
+
});
|
|
4711
|
+
return INVALID;
|
|
4712
|
+
}
|
|
4713
|
+
let ctx = undefined;
|
|
4714
|
+
const status = new ParseStatus();
|
|
4715
|
+
for (const check of this._def.checks) {
|
|
4716
|
+
if (check.kind === "int") {
|
|
4717
|
+
if (!util.isInteger(input.data)) {
|
|
4718
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4719
|
+
addIssueToContext(ctx, {
|
|
4720
|
+
code: ZodIssueCode.invalid_type,
|
|
4721
|
+
expected: "integer",
|
|
4722
|
+
received: "float",
|
|
4723
|
+
message: check.message,
|
|
4724
|
+
});
|
|
4725
|
+
status.dirty();
|
|
4726
|
+
}
|
|
4727
|
+
}
|
|
4728
|
+
else if (check.kind === "min") {
|
|
4729
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
4730
|
+
if (tooSmall) {
|
|
4731
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4732
|
+
addIssueToContext(ctx, {
|
|
4733
|
+
code: ZodIssueCode.too_small,
|
|
4734
|
+
minimum: check.value,
|
|
4735
|
+
type: "number",
|
|
4736
|
+
inclusive: check.inclusive,
|
|
4737
|
+
exact: false,
|
|
4738
|
+
message: check.message,
|
|
4739
|
+
});
|
|
4740
|
+
status.dirty();
|
|
4741
|
+
}
|
|
4742
|
+
}
|
|
4743
|
+
else if (check.kind === "max") {
|
|
4744
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
4745
|
+
if (tooBig) {
|
|
4746
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4747
|
+
addIssueToContext(ctx, {
|
|
4748
|
+
code: ZodIssueCode.too_big,
|
|
4749
|
+
maximum: check.value,
|
|
4750
|
+
type: "number",
|
|
4751
|
+
inclusive: check.inclusive,
|
|
4752
|
+
exact: false,
|
|
4753
|
+
message: check.message,
|
|
4754
|
+
});
|
|
4755
|
+
status.dirty();
|
|
4756
|
+
}
|
|
4757
|
+
}
|
|
4758
|
+
else if (check.kind === "multipleOf") {
|
|
4759
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
4760
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4761
|
+
addIssueToContext(ctx, {
|
|
4762
|
+
code: ZodIssueCode.not_multiple_of,
|
|
4763
|
+
multipleOf: check.value,
|
|
4764
|
+
message: check.message,
|
|
4765
|
+
});
|
|
4766
|
+
status.dirty();
|
|
4767
|
+
}
|
|
4768
|
+
}
|
|
4769
|
+
else if (check.kind === "finite") {
|
|
4770
|
+
if (!Number.isFinite(input.data)) {
|
|
4771
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4772
|
+
addIssueToContext(ctx, {
|
|
4773
|
+
code: ZodIssueCode.not_finite,
|
|
4774
|
+
message: check.message,
|
|
4775
|
+
});
|
|
4776
|
+
status.dirty();
|
|
4777
|
+
}
|
|
4778
|
+
}
|
|
4779
|
+
else {
|
|
4780
|
+
util.assertNever(check);
|
|
4781
|
+
}
|
|
4782
|
+
}
|
|
4783
|
+
return { status: status.value, value: input.data };
|
|
4784
|
+
}
|
|
4785
|
+
gte(value, message) {
|
|
4786
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
4787
|
+
}
|
|
4788
|
+
gt(value, message) {
|
|
4789
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
4790
|
+
}
|
|
4791
|
+
lte(value, message) {
|
|
4792
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
4793
|
+
}
|
|
4794
|
+
lt(value, message) {
|
|
4795
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
4796
|
+
}
|
|
4797
|
+
setLimit(kind, value, inclusive, message) {
|
|
4798
|
+
return new ZodNumber({
|
|
4799
|
+
...this._def,
|
|
4800
|
+
checks: [
|
|
4801
|
+
...this._def.checks,
|
|
4802
|
+
{
|
|
4803
|
+
kind,
|
|
4804
|
+
value,
|
|
4805
|
+
inclusive,
|
|
4806
|
+
message: errorUtil.toString(message),
|
|
4807
|
+
},
|
|
4808
|
+
],
|
|
4809
|
+
});
|
|
4810
|
+
}
|
|
4811
|
+
_addCheck(check) {
|
|
4812
|
+
return new ZodNumber({
|
|
4813
|
+
...this._def,
|
|
4814
|
+
checks: [...this._def.checks, check],
|
|
4815
|
+
});
|
|
4816
|
+
}
|
|
4817
|
+
int(message) {
|
|
4818
|
+
return this._addCheck({
|
|
4819
|
+
kind: "int",
|
|
4820
|
+
message: errorUtil.toString(message),
|
|
4821
|
+
});
|
|
4822
|
+
}
|
|
4823
|
+
positive(message) {
|
|
4824
|
+
return this._addCheck({
|
|
4825
|
+
kind: "min",
|
|
4826
|
+
value: 0,
|
|
4827
|
+
inclusive: false,
|
|
4828
|
+
message: errorUtil.toString(message),
|
|
4829
|
+
});
|
|
4830
|
+
}
|
|
4831
|
+
negative(message) {
|
|
4832
|
+
return this._addCheck({
|
|
4833
|
+
kind: "max",
|
|
4834
|
+
value: 0,
|
|
4835
|
+
inclusive: false,
|
|
4836
|
+
message: errorUtil.toString(message),
|
|
4837
|
+
});
|
|
4838
|
+
}
|
|
4839
|
+
nonpositive(message) {
|
|
4840
|
+
return this._addCheck({
|
|
4841
|
+
kind: "max",
|
|
4842
|
+
value: 0,
|
|
4843
|
+
inclusive: true,
|
|
4844
|
+
message: errorUtil.toString(message),
|
|
4845
|
+
});
|
|
4846
|
+
}
|
|
4847
|
+
nonnegative(message) {
|
|
4848
|
+
return this._addCheck({
|
|
4849
|
+
kind: "min",
|
|
4850
|
+
value: 0,
|
|
4851
|
+
inclusive: true,
|
|
4852
|
+
message: errorUtil.toString(message),
|
|
4853
|
+
});
|
|
4854
|
+
}
|
|
4855
|
+
multipleOf(value, message) {
|
|
4856
|
+
return this._addCheck({
|
|
4857
|
+
kind: "multipleOf",
|
|
4858
|
+
value: value,
|
|
4859
|
+
message: errorUtil.toString(message),
|
|
4860
|
+
});
|
|
4861
|
+
}
|
|
4862
|
+
finite(message) {
|
|
4863
|
+
return this._addCheck({
|
|
4864
|
+
kind: "finite",
|
|
4865
|
+
message: errorUtil.toString(message),
|
|
4866
|
+
});
|
|
4867
|
+
}
|
|
4868
|
+
safe(message) {
|
|
4869
|
+
return this._addCheck({
|
|
4870
|
+
kind: "min",
|
|
4871
|
+
inclusive: true,
|
|
4872
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
4873
|
+
message: errorUtil.toString(message),
|
|
4874
|
+
})._addCheck({
|
|
4875
|
+
kind: "max",
|
|
4876
|
+
inclusive: true,
|
|
4877
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
4878
|
+
message: errorUtil.toString(message),
|
|
4879
|
+
});
|
|
4880
|
+
}
|
|
4881
|
+
get minValue() {
|
|
4882
|
+
let min = null;
|
|
4883
|
+
for (const ch of this._def.checks) {
|
|
4884
|
+
if (ch.kind === "min") {
|
|
4885
|
+
if (min === null || ch.value > min)
|
|
4886
|
+
min = ch.value;
|
|
4887
|
+
}
|
|
4888
|
+
}
|
|
4889
|
+
return min;
|
|
4890
|
+
}
|
|
4891
|
+
get maxValue() {
|
|
4892
|
+
let max = null;
|
|
4893
|
+
for (const ch of this._def.checks) {
|
|
4894
|
+
if (ch.kind === "max") {
|
|
4895
|
+
if (max === null || ch.value < max)
|
|
4896
|
+
max = ch.value;
|
|
4897
|
+
}
|
|
4898
|
+
}
|
|
4899
|
+
return max;
|
|
4900
|
+
}
|
|
4901
|
+
get isInt() {
|
|
4902
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || (ch.kind === "multipleOf" && util.isInteger(ch.value)));
|
|
4903
|
+
}
|
|
4904
|
+
get isFinite() {
|
|
4905
|
+
let max = null;
|
|
4906
|
+
let min = null;
|
|
4907
|
+
for (const ch of this._def.checks) {
|
|
4908
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
4909
|
+
return true;
|
|
4910
|
+
}
|
|
4911
|
+
else if (ch.kind === "min") {
|
|
4912
|
+
if (min === null || ch.value > min)
|
|
4913
|
+
min = ch.value;
|
|
4914
|
+
}
|
|
4915
|
+
else if (ch.kind === "max") {
|
|
4916
|
+
if (max === null || ch.value < max)
|
|
4917
|
+
max = ch.value;
|
|
4918
|
+
}
|
|
4919
|
+
}
|
|
4920
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
4921
|
+
}
|
|
4922
|
+
}
|
|
4923
|
+
ZodNumber.create = (params) => {
|
|
4924
|
+
return new ZodNumber({
|
|
4925
|
+
checks: [],
|
|
4926
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
4927
|
+
coerce: params?.coerce || false,
|
|
4928
|
+
...processCreateParams(params),
|
|
4929
|
+
});
|
|
4930
|
+
};
|
|
4931
|
+
class ZodBigInt extends ZodType {
|
|
4932
|
+
constructor() {
|
|
4933
|
+
super(...arguments);
|
|
4934
|
+
this.min = this.gte;
|
|
4935
|
+
this.max = this.lte;
|
|
4936
|
+
}
|
|
4937
|
+
_parse(input) {
|
|
4938
|
+
if (this._def.coerce) {
|
|
4939
|
+
try {
|
|
4940
|
+
input.data = BigInt(input.data);
|
|
4941
|
+
}
|
|
4942
|
+
catch {
|
|
4943
|
+
return this._getInvalidInput(input);
|
|
4944
|
+
}
|
|
4945
|
+
}
|
|
4946
|
+
const parsedType = this._getType(input);
|
|
4947
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
4948
|
+
return this._getInvalidInput(input);
|
|
4949
|
+
}
|
|
4950
|
+
let ctx = undefined;
|
|
4951
|
+
const status = new ParseStatus();
|
|
4952
|
+
for (const check of this._def.checks) {
|
|
4953
|
+
if (check.kind === "min") {
|
|
4954
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
4955
|
+
if (tooSmall) {
|
|
4956
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4957
|
+
addIssueToContext(ctx, {
|
|
4958
|
+
code: ZodIssueCode.too_small,
|
|
4959
|
+
type: "bigint",
|
|
4960
|
+
minimum: check.value,
|
|
4961
|
+
inclusive: check.inclusive,
|
|
4962
|
+
message: check.message,
|
|
4963
|
+
});
|
|
4964
|
+
status.dirty();
|
|
4965
|
+
}
|
|
4966
|
+
}
|
|
4967
|
+
else if (check.kind === "max") {
|
|
4968
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
4969
|
+
if (tooBig) {
|
|
4970
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4971
|
+
addIssueToContext(ctx, {
|
|
4972
|
+
code: ZodIssueCode.too_big,
|
|
4973
|
+
type: "bigint",
|
|
4974
|
+
maximum: check.value,
|
|
4975
|
+
inclusive: check.inclusive,
|
|
4976
|
+
message: check.message,
|
|
4977
|
+
});
|
|
4978
|
+
status.dirty();
|
|
4979
|
+
}
|
|
4980
|
+
}
|
|
4981
|
+
else if (check.kind === "multipleOf") {
|
|
4982
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
4983
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
4984
|
+
addIssueToContext(ctx, {
|
|
4985
|
+
code: ZodIssueCode.not_multiple_of,
|
|
4986
|
+
multipleOf: check.value,
|
|
4987
|
+
message: check.message,
|
|
4988
|
+
});
|
|
4989
|
+
status.dirty();
|
|
4990
|
+
}
|
|
4991
|
+
}
|
|
4992
|
+
else {
|
|
4993
|
+
util.assertNever(check);
|
|
4994
|
+
}
|
|
4995
|
+
}
|
|
4996
|
+
return { status: status.value, value: input.data };
|
|
4997
|
+
}
|
|
4998
|
+
_getInvalidInput(input) {
|
|
4999
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5000
|
+
addIssueToContext(ctx, {
|
|
5001
|
+
code: ZodIssueCode.invalid_type,
|
|
5002
|
+
expected: ZodParsedType.bigint,
|
|
5003
|
+
received: ctx.parsedType,
|
|
5004
|
+
});
|
|
5005
|
+
return INVALID;
|
|
5006
|
+
}
|
|
5007
|
+
gte(value, message) {
|
|
5008
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
5009
|
+
}
|
|
5010
|
+
gt(value, message) {
|
|
5011
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
5012
|
+
}
|
|
5013
|
+
lte(value, message) {
|
|
5014
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
5015
|
+
}
|
|
5016
|
+
lt(value, message) {
|
|
5017
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
5018
|
+
}
|
|
5019
|
+
setLimit(kind, value, inclusive, message) {
|
|
5020
|
+
return new ZodBigInt({
|
|
5021
|
+
...this._def,
|
|
5022
|
+
checks: [
|
|
5023
|
+
...this._def.checks,
|
|
5024
|
+
{
|
|
5025
|
+
kind,
|
|
5026
|
+
value,
|
|
5027
|
+
inclusive,
|
|
5028
|
+
message: errorUtil.toString(message),
|
|
5029
|
+
},
|
|
5030
|
+
],
|
|
5031
|
+
});
|
|
5032
|
+
}
|
|
5033
|
+
_addCheck(check) {
|
|
5034
|
+
return new ZodBigInt({
|
|
5035
|
+
...this._def,
|
|
5036
|
+
checks: [...this._def.checks, check],
|
|
5037
|
+
});
|
|
5038
|
+
}
|
|
5039
|
+
positive(message) {
|
|
5040
|
+
return this._addCheck({
|
|
5041
|
+
kind: "min",
|
|
5042
|
+
value: BigInt(0),
|
|
5043
|
+
inclusive: false,
|
|
5044
|
+
message: errorUtil.toString(message),
|
|
5045
|
+
});
|
|
5046
|
+
}
|
|
5047
|
+
negative(message) {
|
|
5048
|
+
return this._addCheck({
|
|
5049
|
+
kind: "max",
|
|
5050
|
+
value: BigInt(0),
|
|
5051
|
+
inclusive: false,
|
|
5052
|
+
message: errorUtil.toString(message),
|
|
5053
|
+
});
|
|
5054
|
+
}
|
|
5055
|
+
nonpositive(message) {
|
|
5056
|
+
return this._addCheck({
|
|
5057
|
+
kind: "max",
|
|
5058
|
+
value: BigInt(0),
|
|
5059
|
+
inclusive: true,
|
|
5060
|
+
message: errorUtil.toString(message),
|
|
5061
|
+
});
|
|
5062
|
+
}
|
|
5063
|
+
nonnegative(message) {
|
|
5064
|
+
return this._addCheck({
|
|
5065
|
+
kind: "min",
|
|
5066
|
+
value: BigInt(0),
|
|
5067
|
+
inclusive: true,
|
|
5068
|
+
message: errorUtil.toString(message),
|
|
5069
|
+
});
|
|
5070
|
+
}
|
|
5071
|
+
multipleOf(value, message) {
|
|
5072
|
+
return this._addCheck({
|
|
5073
|
+
kind: "multipleOf",
|
|
5074
|
+
value,
|
|
5075
|
+
message: errorUtil.toString(message),
|
|
5076
|
+
});
|
|
5077
|
+
}
|
|
5078
|
+
get minValue() {
|
|
5079
|
+
let min = null;
|
|
5080
|
+
for (const ch of this._def.checks) {
|
|
5081
|
+
if (ch.kind === "min") {
|
|
5082
|
+
if (min === null || ch.value > min)
|
|
5083
|
+
min = ch.value;
|
|
5084
|
+
}
|
|
5085
|
+
}
|
|
5086
|
+
return min;
|
|
5087
|
+
}
|
|
5088
|
+
get maxValue() {
|
|
5089
|
+
let max = null;
|
|
5090
|
+
for (const ch of this._def.checks) {
|
|
5091
|
+
if (ch.kind === "max") {
|
|
5092
|
+
if (max === null || ch.value < max)
|
|
5093
|
+
max = ch.value;
|
|
5094
|
+
}
|
|
5095
|
+
}
|
|
5096
|
+
return max;
|
|
5097
|
+
}
|
|
5098
|
+
}
|
|
5099
|
+
ZodBigInt.create = (params) => {
|
|
5100
|
+
return new ZodBigInt({
|
|
5101
|
+
checks: [],
|
|
5102
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
5103
|
+
coerce: params?.coerce ?? false,
|
|
5104
|
+
...processCreateParams(params),
|
|
5105
|
+
});
|
|
5106
|
+
};
|
|
5107
|
+
class ZodBoolean extends ZodType {
|
|
5108
|
+
_parse(input) {
|
|
5109
|
+
if (this._def.coerce) {
|
|
5110
|
+
input.data = Boolean(input.data);
|
|
5111
|
+
}
|
|
5112
|
+
const parsedType = this._getType(input);
|
|
5113
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
5114
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5115
|
+
addIssueToContext(ctx, {
|
|
5116
|
+
code: ZodIssueCode.invalid_type,
|
|
5117
|
+
expected: ZodParsedType.boolean,
|
|
5118
|
+
received: ctx.parsedType,
|
|
5119
|
+
});
|
|
5120
|
+
return INVALID;
|
|
5121
|
+
}
|
|
5122
|
+
return OK(input.data);
|
|
5123
|
+
}
|
|
5124
|
+
}
|
|
5125
|
+
ZodBoolean.create = (params) => {
|
|
5126
|
+
return new ZodBoolean({
|
|
5127
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
5128
|
+
coerce: params?.coerce || false,
|
|
5129
|
+
...processCreateParams(params),
|
|
5130
|
+
});
|
|
5131
|
+
};
|
|
5132
|
+
class ZodDate extends ZodType {
|
|
5133
|
+
_parse(input) {
|
|
5134
|
+
if (this._def.coerce) {
|
|
5135
|
+
input.data = new Date(input.data);
|
|
5136
|
+
}
|
|
5137
|
+
const parsedType = this._getType(input);
|
|
5138
|
+
if (parsedType !== ZodParsedType.date) {
|
|
5139
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5140
|
+
addIssueToContext(ctx, {
|
|
5141
|
+
code: ZodIssueCode.invalid_type,
|
|
5142
|
+
expected: ZodParsedType.date,
|
|
5143
|
+
received: ctx.parsedType,
|
|
5144
|
+
});
|
|
5145
|
+
return INVALID;
|
|
5146
|
+
}
|
|
5147
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
5148
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5149
|
+
addIssueToContext(ctx, {
|
|
5150
|
+
code: ZodIssueCode.invalid_date,
|
|
5151
|
+
});
|
|
5152
|
+
return INVALID;
|
|
5153
|
+
}
|
|
5154
|
+
const status = new ParseStatus();
|
|
5155
|
+
let ctx = undefined;
|
|
5156
|
+
for (const check of this._def.checks) {
|
|
5157
|
+
if (check.kind === "min") {
|
|
5158
|
+
if (input.data.getTime() < check.value) {
|
|
5159
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
5160
|
+
addIssueToContext(ctx, {
|
|
5161
|
+
code: ZodIssueCode.too_small,
|
|
5162
|
+
message: check.message,
|
|
5163
|
+
inclusive: true,
|
|
5164
|
+
exact: false,
|
|
5165
|
+
minimum: check.value,
|
|
5166
|
+
type: "date",
|
|
5167
|
+
});
|
|
5168
|
+
status.dirty();
|
|
5169
|
+
}
|
|
5170
|
+
}
|
|
5171
|
+
else if (check.kind === "max") {
|
|
5172
|
+
if (input.data.getTime() > check.value) {
|
|
5173
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
5174
|
+
addIssueToContext(ctx, {
|
|
5175
|
+
code: ZodIssueCode.too_big,
|
|
5176
|
+
message: check.message,
|
|
5177
|
+
inclusive: true,
|
|
5178
|
+
exact: false,
|
|
5179
|
+
maximum: check.value,
|
|
5180
|
+
type: "date",
|
|
5181
|
+
});
|
|
5182
|
+
status.dirty();
|
|
5183
|
+
}
|
|
5184
|
+
}
|
|
5185
|
+
else {
|
|
5186
|
+
util.assertNever(check);
|
|
5187
|
+
}
|
|
5188
|
+
}
|
|
5189
|
+
return {
|
|
5190
|
+
status: status.value,
|
|
5191
|
+
value: new Date(input.data.getTime()),
|
|
5192
|
+
};
|
|
5193
|
+
}
|
|
5194
|
+
_addCheck(check) {
|
|
5195
|
+
return new ZodDate({
|
|
5196
|
+
...this._def,
|
|
5197
|
+
checks: [...this._def.checks, check],
|
|
5198
|
+
});
|
|
5199
|
+
}
|
|
5200
|
+
min(minDate, message) {
|
|
5201
|
+
return this._addCheck({
|
|
5202
|
+
kind: "min",
|
|
5203
|
+
value: minDate.getTime(),
|
|
5204
|
+
message: errorUtil.toString(message),
|
|
5205
|
+
});
|
|
5206
|
+
}
|
|
5207
|
+
max(maxDate, message) {
|
|
5208
|
+
return this._addCheck({
|
|
5209
|
+
kind: "max",
|
|
5210
|
+
value: maxDate.getTime(),
|
|
5211
|
+
message: errorUtil.toString(message),
|
|
5212
|
+
});
|
|
5213
|
+
}
|
|
5214
|
+
get minDate() {
|
|
5215
|
+
let min = null;
|
|
5216
|
+
for (const ch of this._def.checks) {
|
|
5217
|
+
if (ch.kind === "min") {
|
|
5218
|
+
if (min === null || ch.value > min)
|
|
5219
|
+
min = ch.value;
|
|
5220
|
+
}
|
|
5221
|
+
}
|
|
5222
|
+
return min != null ? new Date(min) : null;
|
|
5223
|
+
}
|
|
5224
|
+
get maxDate() {
|
|
5225
|
+
let max = null;
|
|
5226
|
+
for (const ch of this._def.checks) {
|
|
5227
|
+
if (ch.kind === "max") {
|
|
5228
|
+
if (max === null || ch.value < max)
|
|
5229
|
+
max = ch.value;
|
|
5230
|
+
}
|
|
5231
|
+
}
|
|
5232
|
+
return max != null ? new Date(max) : null;
|
|
5233
|
+
}
|
|
5234
|
+
}
|
|
5235
|
+
ZodDate.create = (params) => {
|
|
5236
|
+
return new ZodDate({
|
|
5237
|
+
checks: [],
|
|
5238
|
+
coerce: params?.coerce || false,
|
|
5239
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
5240
|
+
...processCreateParams(params),
|
|
5241
|
+
});
|
|
5242
|
+
};
|
|
5243
|
+
class ZodSymbol extends ZodType {
|
|
5244
|
+
_parse(input) {
|
|
5245
|
+
const parsedType = this._getType(input);
|
|
5246
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
5247
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5248
|
+
addIssueToContext(ctx, {
|
|
5249
|
+
code: ZodIssueCode.invalid_type,
|
|
5250
|
+
expected: ZodParsedType.symbol,
|
|
5251
|
+
received: ctx.parsedType,
|
|
5252
|
+
});
|
|
5253
|
+
return INVALID;
|
|
5254
|
+
}
|
|
5255
|
+
return OK(input.data);
|
|
5256
|
+
}
|
|
5257
|
+
}
|
|
5258
|
+
ZodSymbol.create = (params) => {
|
|
5259
|
+
return new ZodSymbol({
|
|
5260
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
5261
|
+
...processCreateParams(params),
|
|
5262
|
+
});
|
|
5263
|
+
};
|
|
5264
|
+
class ZodUndefined extends ZodType {
|
|
5265
|
+
_parse(input) {
|
|
5266
|
+
const parsedType = this._getType(input);
|
|
5267
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
5268
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5269
|
+
addIssueToContext(ctx, {
|
|
5270
|
+
code: ZodIssueCode.invalid_type,
|
|
5271
|
+
expected: ZodParsedType.undefined,
|
|
5272
|
+
received: ctx.parsedType,
|
|
5273
|
+
});
|
|
5274
|
+
return INVALID;
|
|
5275
|
+
}
|
|
5276
|
+
return OK(input.data);
|
|
5277
|
+
}
|
|
5278
|
+
}
|
|
5279
|
+
ZodUndefined.create = (params) => {
|
|
5280
|
+
return new ZodUndefined({
|
|
5281
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
5282
|
+
...processCreateParams(params),
|
|
5283
|
+
});
|
|
5284
|
+
};
|
|
5285
|
+
class ZodNull extends ZodType {
|
|
5286
|
+
_parse(input) {
|
|
5287
|
+
const parsedType = this._getType(input);
|
|
5288
|
+
if (parsedType !== ZodParsedType.null) {
|
|
5289
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5290
|
+
addIssueToContext(ctx, {
|
|
5291
|
+
code: ZodIssueCode.invalid_type,
|
|
5292
|
+
expected: ZodParsedType.null,
|
|
5293
|
+
received: ctx.parsedType,
|
|
5294
|
+
});
|
|
5295
|
+
return INVALID;
|
|
5296
|
+
}
|
|
5297
|
+
return OK(input.data);
|
|
5298
|
+
}
|
|
5299
|
+
}
|
|
5300
|
+
ZodNull.create = (params) => {
|
|
5301
|
+
return new ZodNull({
|
|
5302
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
5303
|
+
...processCreateParams(params),
|
|
5304
|
+
});
|
|
5305
|
+
};
|
|
5306
|
+
class ZodAny extends ZodType {
|
|
5307
|
+
constructor() {
|
|
5308
|
+
super(...arguments);
|
|
5309
|
+
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
5310
|
+
this._any = true;
|
|
5311
|
+
}
|
|
5312
|
+
_parse(input) {
|
|
5313
|
+
return OK(input.data);
|
|
5314
|
+
}
|
|
5315
|
+
}
|
|
5316
|
+
ZodAny.create = (params) => {
|
|
5317
|
+
return new ZodAny({
|
|
5318
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
5319
|
+
...processCreateParams(params),
|
|
5320
|
+
});
|
|
5321
|
+
};
|
|
5322
|
+
class ZodUnknown extends ZodType {
|
|
5323
|
+
constructor() {
|
|
5324
|
+
super(...arguments);
|
|
5325
|
+
// required
|
|
5326
|
+
this._unknown = true;
|
|
5327
|
+
}
|
|
5328
|
+
_parse(input) {
|
|
5329
|
+
return OK(input.data);
|
|
5330
|
+
}
|
|
5331
|
+
}
|
|
5332
|
+
ZodUnknown.create = (params) => {
|
|
5333
|
+
return new ZodUnknown({
|
|
5334
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
5335
|
+
...processCreateParams(params),
|
|
5336
|
+
});
|
|
5337
|
+
};
|
|
5338
|
+
class ZodNever extends ZodType {
|
|
5339
|
+
_parse(input) {
|
|
5340
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5341
|
+
addIssueToContext(ctx, {
|
|
5342
|
+
code: ZodIssueCode.invalid_type,
|
|
5343
|
+
expected: ZodParsedType.never,
|
|
5344
|
+
received: ctx.parsedType,
|
|
5345
|
+
});
|
|
5346
|
+
return INVALID;
|
|
5347
|
+
}
|
|
5348
|
+
}
|
|
5349
|
+
ZodNever.create = (params) => {
|
|
5350
|
+
return new ZodNever({
|
|
5351
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
5352
|
+
...processCreateParams(params),
|
|
5353
|
+
});
|
|
5354
|
+
};
|
|
5355
|
+
class ZodVoid extends ZodType {
|
|
5356
|
+
_parse(input) {
|
|
5357
|
+
const parsedType = this._getType(input);
|
|
5358
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
5359
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5360
|
+
addIssueToContext(ctx, {
|
|
5361
|
+
code: ZodIssueCode.invalid_type,
|
|
5362
|
+
expected: ZodParsedType.void,
|
|
5363
|
+
received: ctx.parsedType,
|
|
5364
|
+
});
|
|
5365
|
+
return INVALID;
|
|
5366
|
+
}
|
|
5367
|
+
return OK(input.data);
|
|
5368
|
+
}
|
|
5369
|
+
}
|
|
5370
|
+
ZodVoid.create = (params) => {
|
|
5371
|
+
return new ZodVoid({
|
|
5372
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
5373
|
+
...processCreateParams(params),
|
|
5374
|
+
});
|
|
5375
|
+
};
|
|
5376
|
+
class ZodArray extends ZodType {
|
|
5377
|
+
_parse(input) {
|
|
5378
|
+
const { ctx, status } = this._processInputParams(input);
|
|
5379
|
+
const def = this._def;
|
|
5380
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
5381
|
+
addIssueToContext(ctx, {
|
|
5382
|
+
code: ZodIssueCode.invalid_type,
|
|
5383
|
+
expected: ZodParsedType.array,
|
|
5384
|
+
received: ctx.parsedType,
|
|
5385
|
+
});
|
|
5386
|
+
return INVALID;
|
|
5387
|
+
}
|
|
5388
|
+
if (def.exactLength !== null) {
|
|
5389
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
5390
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
5391
|
+
if (tooBig || tooSmall) {
|
|
5392
|
+
addIssueToContext(ctx, {
|
|
5393
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
5394
|
+
minimum: (tooSmall ? def.exactLength.value : undefined),
|
|
5395
|
+
maximum: (tooBig ? def.exactLength.value : undefined),
|
|
5396
|
+
type: "array",
|
|
5397
|
+
inclusive: true,
|
|
5398
|
+
exact: true,
|
|
5399
|
+
message: def.exactLength.message,
|
|
5400
|
+
});
|
|
5401
|
+
status.dirty();
|
|
5402
|
+
}
|
|
5403
|
+
}
|
|
5404
|
+
if (def.minLength !== null) {
|
|
5405
|
+
if (ctx.data.length < def.minLength.value) {
|
|
5406
|
+
addIssueToContext(ctx, {
|
|
5407
|
+
code: ZodIssueCode.too_small,
|
|
5408
|
+
minimum: def.minLength.value,
|
|
5409
|
+
type: "array",
|
|
5410
|
+
inclusive: true,
|
|
5411
|
+
exact: false,
|
|
5412
|
+
message: def.minLength.message,
|
|
5413
|
+
});
|
|
5414
|
+
status.dirty();
|
|
5415
|
+
}
|
|
5416
|
+
}
|
|
5417
|
+
if (def.maxLength !== null) {
|
|
5418
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
5419
|
+
addIssueToContext(ctx, {
|
|
5420
|
+
code: ZodIssueCode.too_big,
|
|
5421
|
+
maximum: def.maxLength.value,
|
|
5422
|
+
type: "array",
|
|
5423
|
+
inclusive: true,
|
|
5424
|
+
exact: false,
|
|
5425
|
+
message: def.maxLength.message,
|
|
5426
|
+
});
|
|
5427
|
+
status.dirty();
|
|
5428
|
+
}
|
|
5429
|
+
}
|
|
5430
|
+
if (ctx.common.async) {
|
|
5431
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
5432
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
5433
|
+
})).then((result) => {
|
|
5434
|
+
return ParseStatus.mergeArray(status, result);
|
|
5435
|
+
});
|
|
5436
|
+
}
|
|
5437
|
+
const result = [...ctx.data].map((item, i) => {
|
|
5438
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
5439
|
+
});
|
|
5440
|
+
return ParseStatus.mergeArray(status, result);
|
|
5441
|
+
}
|
|
5442
|
+
get element() {
|
|
5443
|
+
return this._def.type;
|
|
5444
|
+
}
|
|
5445
|
+
min(minLength, message) {
|
|
5446
|
+
return new ZodArray({
|
|
5447
|
+
...this._def,
|
|
5448
|
+
minLength: { value: minLength, message: errorUtil.toString(message) },
|
|
5449
|
+
});
|
|
5450
|
+
}
|
|
5451
|
+
max(maxLength, message) {
|
|
5452
|
+
return new ZodArray({
|
|
5453
|
+
...this._def,
|
|
5454
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) },
|
|
5455
|
+
});
|
|
5456
|
+
}
|
|
5457
|
+
length(len, message) {
|
|
5458
|
+
return new ZodArray({
|
|
5459
|
+
...this._def,
|
|
5460
|
+
exactLength: { value: len, message: errorUtil.toString(message) },
|
|
5461
|
+
});
|
|
5462
|
+
}
|
|
5463
|
+
nonempty(message) {
|
|
5464
|
+
return this.min(1, message);
|
|
5465
|
+
}
|
|
5466
|
+
}
|
|
5467
|
+
ZodArray.create = (schema, params) => {
|
|
5468
|
+
return new ZodArray({
|
|
5469
|
+
type: schema,
|
|
5470
|
+
minLength: null,
|
|
5471
|
+
maxLength: null,
|
|
5472
|
+
exactLength: null,
|
|
5473
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
5474
|
+
...processCreateParams(params),
|
|
5475
|
+
});
|
|
5476
|
+
};
|
|
5477
|
+
function deepPartialify(schema) {
|
|
5478
|
+
if (schema instanceof ZodObject) {
|
|
5479
|
+
const newShape = {};
|
|
5480
|
+
for (const key in schema.shape) {
|
|
5481
|
+
const fieldSchema = schema.shape[key];
|
|
5482
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
5483
|
+
}
|
|
5484
|
+
return new ZodObject({
|
|
5485
|
+
...schema._def,
|
|
5486
|
+
shape: () => newShape,
|
|
5487
|
+
});
|
|
5488
|
+
}
|
|
5489
|
+
else if (schema instanceof ZodArray) {
|
|
5490
|
+
return new ZodArray({
|
|
5491
|
+
...schema._def,
|
|
5492
|
+
type: deepPartialify(schema.element),
|
|
5493
|
+
});
|
|
5494
|
+
}
|
|
5495
|
+
else if (schema instanceof ZodOptional) {
|
|
5496
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
5497
|
+
}
|
|
5498
|
+
else if (schema instanceof ZodNullable) {
|
|
5499
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
5500
|
+
}
|
|
5501
|
+
else if (schema instanceof ZodTuple) {
|
|
5502
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
5503
|
+
}
|
|
5504
|
+
else {
|
|
5505
|
+
return schema;
|
|
5506
|
+
}
|
|
5507
|
+
}
|
|
5508
|
+
class ZodObject extends ZodType {
|
|
5509
|
+
constructor() {
|
|
5510
|
+
super(...arguments);
|
|
5511
|
+
this._cached = null;
|
|
5512
|
+
/**
|
|
5513
|
+
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
5514
|
+
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
5515
|
+
*/
|
|
5516
|
+
this.nonstrict = this.passthrough;
|
|
5517
|
+
// extend<
|
|
5518
|
+
// Augmentation extends ZodRawShape,
|
|
5519
|
+
// NewOutput extends util.flatten<{
|
|
5520
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
5521
|
+
// ? Augmentation[k]["_output"]
|
|
5522
|
+
// : k extends keyof Output
|
|
5523
|
+
// ? Output[k]
|
|
5524
|
+
// : never;
|
|
5525
|
+
// }>,
|
|
5526
|
+
// NewInput extends util.flatten<{
|
|
5527
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
5528
|
+
// ? Augmentation[k]["_input"]
|
|
5529
|
+
// : k extends keyof Input
|
|
5530
|
+
// ? Input[k]
|
|
5531
|
+
// : never;
|
|
5532
|
+
// }>
|
|
5533
|
+
// >(
|
|
5534
|
+
// augmentation: Augmentation
|
|
5535
|
+
// ): ZodObject<
|
|
5536
|
+
// extendShape<T, Augmentation>,
|
|
5537
|
+
// UnknownKeys,
|
|
5538
|
+
// Catchall,
|
|
5539
|
+
// NewOutput,
|
|
5540
|
+
// NewInput
|
|
5541
|
+
// > {
|
|
5542
|
+
// return new ZodObject({
|
|
5543
|
+
// ...this._def,
|
|
5544
|
+
// shape: () => ({
|
|
5545
|
+
// ...this._def.shape(),
|
|
5546
|
+
// ...augmentation,
|
|
5547
|
+
// }),
|
|
5548
|
+
// }) as any;
|
|
5549
|
+
// }
|
|
5550
|
+
/**
|
|
5551
|
+
* @deprecated Use `.extend` instead
|
|
5552
|
+
* */
|
|
5553
|
+
this.augment = this.extend;
|
|
5554
|
+
}
|
|
5555
|
+
_getCached() {
|
|
5556
|
+
if (this._cached !== null)
|
|
5557
|
+
return this._cached;
|
|
5558
|
+
const shape = this._def.shape();
|
|
5559
|
+
const keys = util.objectKeys(shape);
|
|
5560
|
+
this._cached = { shape, keys };
|
|
5561
|
+
return this._cached;
|
|
5562
|
+
}
|
|
5563
|
+
_parse(input) {
|
|
5564
|
+
const parsedType = this._getType(input);
|
|
5565
|
+
if (parsedType !== ZodParsedType.object) {
|
|
5566
|
+
const ctx = this._getOrReturnCtx(input);
|
|
5567
|
+
addIssueToContext(ctx, {
|
|
5568
|
+
code: ZodIssueCode.invalid_type,
|
|
5569
|
+
expected: ZodParsedType.object,
|
|
5570
|
+
received: ctx.parsedType,
|
|
5571
|
+
});
|
|
5572
|
+
return INVALID;
|
|
5573
|
+
}
|
|
5574
|
+
const { status, ctx } = this._processInputParams(input);
|
|
5575
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
5576
|
+
const extraKeys = [];
|
|
5577
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
5578
|
+
for (const key in ctx.data) {
|
|
5579
|
+
if (!shapeKeys.includes(key)) {
|
|
5580
|
+
extraKeys.push(key);
|
|
5581
|
+
}
|
|
5582
|
+
}
|
|
5583
|
+
}
|
|
5584
|
+
const pairs = [];
|
|
5585
|
+
for (const key of shapeKeys) {
|
|
5586
|
+
const keyValidator = shape[key];
|
|
5587
|
+
const value = ctx.data[key];
|
|
5588
|
+
pairs.push({
|
|
5589
|
+
key: { status: "valid", value: key },
|
|
5590
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
5591
|
+
alwaysSet: key in ctx.data,
|
|
5592
|
+
});
|
|
5593
|
+
}
|
|
5594
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
5595
|
+
const unknownKeys = this._def.unknownKeys;
|
|
5596
|
+
if (unknownKeys === "passthrough") {
|
|
5597
|
+
for (const key of extraKeys) {
|
|
5598
|
+
pairs.push({
|
|
5599
|
+
key: { status: "valid", value: key },
|
|
5600
|
+
value: { status: "valid", value: ctx.data[key] },
|
|
5601
|
+
});
|
|
5602
|
+
}
|
|
5603
|
+
}
|
|
5604
|
+
else if (unknownKeys === "strict") {
|
|
5605
|
+
if (extraKeys.length > 0) {
|
|
5606
|
+
addIssueToContext(ctx, {
|
|
5607
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
5608
|
+
keys: extraKeys,
|
|
5609
|
+
});
|
|
5610
|
+
status.dirty();
|
|
5611
|
+
}
|
|
5612
|
+
}
|
|
5613
|
+
else if (unknownKeys === "strip") ;
|
|
5614
|
+
else {
|
|
5615
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
5616
|
+
}
|
|
5617
|
+
}
|
|
5618
|
+
else {
|
|
5619
|
+
// run catchall validation
|
|
5620
|
+
const catchall = this._def.catchall;
|
|
5621
|
+
for (const key of extraKeys) {
|
|
5622
|
+
const value = ctx.data[key];
|
|
5623
|
+
pairs.push({
|
|
5624
|
+
key: { status: "valid", value: key },
|
|
5625
|
+
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
|
|
5626
|
+
),
|
|
5627
|
+
alwaysSet: key in ctx.data,
|
|
5628
|
+
});
|
|
5629
|
+
}
|
|
5630
|
+
}
|
|
5631
|
+
if (ctx.common.async) {
|
|
5632
|
+
return Promise.resolve()
|
|
5633
|
+
.then(async () => {
|
|
5634
|
+
const syncPairs = [];
|
|
5635
|
+
for (const pair of pairs) {
|
|
5636
|
+
const key = await pair.key;
|
|
5637
|
+
const value = await pair.value;
|
|
5638
|
+
syncPairs.push({
|
|
5639
|
+
key,
|
|
5640
|
+
value,
|
|
5641
|
+
alwaysSet: pair.alwaysSet,
|
|
5642
|
+
});
|
|
5643
|
+
}
|
|
5644
|
+
return syncPairs;
|
|
5645
|
+
})
|
|
5646
|
+
.then((syncPairs) => {
|
|
5647
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
5648
|
+
});
|
|
5649
|
+
}
|
|
5650
|
+
else {
|
|
5651
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
5652
|
+
}
|
|
5653
|
+
}
|
|
5654
|
+
get shape() {
|
|
5655
|
+
return this._def.shape();
|
|
5656
|
+
}
|
|
5657
|
+
strict(message) {
|
|
5658
|
+
errorUtil.errToObj;
|
|
5659
|
+
return new ZodObject({
|
|
5660
|
+
...this._def,
|
|
5661
|
+
unknownKeys: "strict",
|
|
5662
|
+
...(message !== undefined
|
|
5663
|
+
? {
|
|
5664
|
+
errorMap: (issue, ctx) => {
|
|
5665
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
5666
|
+
if (issue.code === "unrecognized_keys")
|
|
5667
|
+
return {
|
|
5668
|
+
message: errorUtil.errToObj(message).message ?? defaultError,
|
|
5669
|
+
};
|
|
5670
|
+
return {
|
|
5671
|
+
message: defaultError,
|
|
5672
|
+
};
|
|
5673
|
+
},
|
|
5674
|
+
}
|
|
5675
|
+
: {}),
|
|
5676
|
+
});
|
|
5677
|
+
}
|
|
5678
|
+
strip() {
|
|
5679
|
+
return new ZodObject({
|
|
5680
|
+
...this._def,
|
|
5681
|
+
unknownKeys: "strip",
|
|
5682
|
+
});
|
|
5683
|
+
}
|
|
5684
|
+
passthrough() {
|
|
5685
|
+
return new ZodObject({
|
|
5686
|
+
...this._def,
|
|
5687
|
+
unknownKeys: "passthrough",
|
|
5688
|
+
});
|
|
5689
|
+
}
|
|
5690
|
+
// const AugmentFactory =
|
|
5691
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
5692
|
+
// <Augmentation extends ZodRawShape>(
|
|
5693
|
+
// augmentation: Augmentation
|
|
5694
|
+
// ): ZodObject<
|
|
5695
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
5696
|
+
// Def["unknownKeys"],
|
|
5697
|
+
// Def["catchall"]
|
|
5698
|
+
// > => {
|
|
5699
|
+
// return new ZodObject({
|
|
5700
|
+
// ...def,
|
|
5701
|
+
// shape: () => ({
|
|
5702
|
+
// ...def.shape(),
|
|
5703
|
+
// ...augmentation,
|
|
5704
|
+
// }),
|
|
5705
|
+
// }) as any;
|
|
5706
|
+
// };
|
|
5707
|
+
extend(augmentation) {
|
|
5708
|
+
return new ZodObject({
|
|
5709
|
+
...this._def,
|
|
5710
|
+
shape: () => ({
|
|
5711
|
+
...this._def.shape(),
|
|
5712
|
+
...augmentation,
|
|
5713
|
+
}),
|
|
5714
|
+
});
|
|
5715
|
+
}
|
|
5716
|
+
/**
|
|
5717
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
5718
|
+
* inferred type of merged objects. Please
|
|
5719
|
+
* upgrade if you are experiencing issues.
|
|
5720
|
+
*/
|
|
5721
|
+
merge(merging) {
|
|
5722
|
+
const merged = new ZodObject({
|
|
5723
|
+
unknownKeys: merging._def.unknownKeys,
|
|
5724
|
+
catchall: merging._def.catchall,
|
|
5725
|
+
shape: () => ({
|
|
5726
|
+
...this._def.shape(),
|
|
5727
|
+
...merging._def.shape(),
|
|
5728
|
+
}),
|
|
5729
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5730
|
+
});
|
|
5731
|
+
return merged;
|
|
5732
|
+
}
|
|
5733
|
+
// merge<
|
|
5734
|
+
// Incoming extends AnyZodObject,
|
|
5735
|
+
// Augmentation extends Incoming["shape"],
|
|
5736
|
+
// NewOutput extends {
|
|
5737
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
5738
|
+
// ? Augmentation[k]["_output"]
|
|
5739
|
+
// : k extends keyof Output
|
|
5740
|
+
// ? Output[k]
|
|
5741
|
+
// : never;
|
|
5742
|
+
// },
|
|
5743
|
+
// NewInput extends {
|
|
5744
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
5745
|
+
// ? Augmentation[k]["_input"]
|
|
5746
|
+
// : k extends keyof Input
|
|
5747
|
+
// ? Input[k]
|
|
5748
|
+
// : never;
|
|
5749
|
+
// }
|
|
5750
|
+
// >(
|
|
5751
|
+
// merging: Incoming
|
|
5752
|
+
// ): ZodObject<
|
|
5753
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
5754
|
+
// Incoming["_def"]["unknownKeys"],
|
|
5755
|
+
// Incoming["_def"]["catchall"],
|
|
5756
|
+
// NewOutput,
|
|
5757
|
+
// NewInput
|
|
5758
|
+
// > {
|
|
5759
|
+
// const merged: any = new ZodObject({
|
|
5760
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
5761
|
+
// catchall: merging._def.catchall,
|
|
5762
|
+
// shape: () =>
|
|
5763
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
5764
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5765
|
+
// }) as any;
|
|
5766
|
+
// return merged;
|
|
5767
|
+
// }
|
|
5768
|
+
setKey(key, schema) {
|
|
5769
|
+
return this.augment({ [key]: schema });
|
|
5770
|
+
}
|
|
5771
|
+
// merge<Incoming extends AnyZodObject>(
|
|
5772
|
+
// merging: Incoming
|
|
5773
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
5774
|
+
// ZodObject<
|
|
5775
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
5776
|
+
// Incoming["_def"]["unknownKeys"],
|
|
5777
|
+
// Incoming["_def"]["catchall"]
|
|
5778
|
+
// > {
|
|
5779
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
5780
|
+
// // this._def.shape(),
|
|
5781
|
+
// // merging._def.shape()
|
|
5782
|
+
// // );
|
|
5783
|
+
// const merged: any = new ZodObject({
|
|
5784
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
5785
|
+
// catchall: merging._def.catchall,
|
|
5786
|
+
// shape: () =>
|
|
5787
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
5788
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5789
|
+
// }) as any;
|
|
5790
|
+
// return merged;
|
|
5791
|
+
// }
|
|
5792
|
+
catchall(index) {
|
|
5793
|
+
return new ZodObject({
|
|
5794
|
+
...this._def,
|
|
5795
|
+
catchall: index,
|
|
5796
|
+
});
|
|
5797
|
+
}
|
|
5798
|
+
pick(mask) {
|
|
5799
|
+
const shape = {};
|
|
5800
|
+
for (const key of util.objectKeys(mask)) {
|
|
5801
|
+
if (mask[key] && this.shape[key]) {
|
|
5802
|
+
shape[key] = this.shape[key];
|
|
5803
|
+
}
|
|
5804
|
+
}
|
|
5805
|
+
return new ZodObject({
|
|
5806
|
+
...this._def,
|
|
5807
|
+
shape: () => shape,
|
|
5808
|
+
});
|
|
5809
|
+
}
|
|
5810
|
+
omit(mask) {
|
|
5811
|
+
const shape = {};
|
|
5812
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
5813
|
+
if (!mask[key]) {
|
|
5814
|
+
shape[key] = this.shape[key];
|
|
5815
|
+
}
|
|
5816
|
+
}
|
|
5817
|
+
return new ZodObject({
|
|
5818
|
+
...this._def,
|
|
5819
|
+
shape: () => shape,
|
|
5820
|
+
});
|
|
5821
|
+
}
|
|
5822
|
+
/**
|
|
5823
|
+
* @deprecated
|
|
5824
|
+
*/
|
|
5825
|
+
deepPartial() {
|
|
5826
|
+
return deepPartialify(this);
|
|
5827
|
+
}
|
|
5828
|
+
partial(mask) {
|
|
5829
|
+
const newShape = {};
|
|
5830
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
5831
|
+
const fieldSchema = this.shape[key];
|
|
5832
|
+
if (mask && !mask[key]) {
|
|
5833
|
+
newShape[key] = fieldSchema;
|
|
5834
|
+
}
|
|
5835
|
+
else {
|
|
5836
|
+
newShape[key] = fieldSchema.optional();
|
|
5837
|
+
}
|
|
5838
|
+
}
|
|
5839
|
+
return new ZodObject({
|
|
5840
|
+
...this._def,
|
|
5841
|
+
shape: () => newShape,
|
|
5842
|
+
});
|
|
5843
|
+
}
|
|
5844
|
+
required(mask) {
|
|
5845
|
+
const newShape = {};
|
|
5846
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
5847
|
+
if (mask && !mask[key]) {
|
|
5848
|
+
newShape[key] = this.shape[key];
|
|
5849
|
+
}
|
|
5850
|
+
else {
|
|
5851
|
+
const fieldSchema = this.shape[key];
|
|
5852
|
+
let newField = fieldSchema;
|
|
5853
|
+
while (newField instanceof ZodOptional) {
|
|
5854
|
+
newField = newField._def.innerType;
|
|
5855
|
+
}
|
|
5856
|
+
newShape[key] = newField;
|
|
5857
|
+
}
|
|
5858
|
+
}
|
|
5859
|
+
return new ZodObject({
|
|
5860
|
+
...this._def,
|
|
5861
|
+
shape: () => newShape,
|
|
5862
|
+
});
|
|
5863
|
+
}
|
|
5864
|
+
keyof() {
|
|
5865
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
5866
|
+
}
|
|
5867
|
+
}
|
|
5868
|
+
ZodObject.create = (shape, params) => {
|
|
5869
|
+
return new ZodObject({
|
|
5870
|
+
shape: () => shape,
|
|
5871
|
+
unknownKeys: "strip",
|
|
5872
|
+
catchall: ZodNever.create(),
|
|
5873
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5874
|
+
...processCreateParams(params),
|
|
5875
|
+
});
|
|
5876
|
+
};
|
|
5877
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
5878
|
+
return new ZodObject({
|
|
5879
|
+
shape: () => shape,
|
|
5880
|
+
unknownKeys: "strict",
|
|
5881
|
+
catchall: ZodNever.create(),
|
|
5882
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5883
|
+
...processCreateParams(params),
|
|
5884
|
+
});
|
|
5885
|
+
};
|
|
5886
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
5887
|
+
return new ZodObject({
|
|
5888
|
+
shape,
|
|
5889
|
+
unknownKeys: "strip",
|
|
5890
|
+
catchall: ZodNever.create(),
|
|
5891
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
5892
|
+
...processCreateParams(params),
|
|
5893
|
+
});
|
|
5894
|
+
};
|
|
5895
|
+
class ZodUnion extends ZodType {
|
|
5896
|
+
_parse(input) {
|
|
5897
|
+
const { ctx } = this._processInputParams(input);
|
|
5898
|
+
const options = this._def.options;
|
|
5899
|
+
function handleResults(results) {
|
|
5900
|
+
// return first issue-free validation if it exists
|
|
5901
|
+
for (const result of results) {
|
|
5902
|
+
if (result.result.status === "valid") {
|
|
5903
|
+
return result.result;
|
|
5904
|
+
}
|
|
5905
|
+
}
|
|
5906
|
+
for (const result of results) {
|
|
5907
|
+
if (result.result.status === "dirty") {
|
|
5908
|
+
// add issues from dirty option
|
|
5909
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
5910
|
+
return result.result;
|
|
5911
|
+
}
|
|
5912
|
+
}
|
|
5913
|
+
// return invalid
|
|
5914
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
5915
|
+
addIssueToContext(ctx, {
|
|
5916
|
+
code: ZodIssueCode.invalid_union,
|
|
5917
|
+
unionErrors,
|
|
5918
|
+
});
|
|
5919
|
+
return INVALID;
|
|
5920
|
+
}
|
|
5921
|
+
if (ctx.common.async) {
|
|
5922
|
+
return Promise.all(options.map(async (option) => {
|
|
5923
|
+
const childCtx = {
|
|
5924
|
+
...ctx,
|
|
5925
|
+
common: {
|
|
5926
|
+
...ctx.common,
|
|
5927
|
+
issues: [],
|
|
5928
|
+
},
|
|
5929
|
+
parent: null,
|
|
5930
|
+
};
|
|
5931
|
+
return {
|
|
5932
|
+
result: await option._parseAsync({
|
|
5933
|
+
data: ctx.data,
|
|
5934
|
+
path: ctx.path,
|
|
5935
|
+
parent: childCtx,
|
|
5936
|
+
}),
|
|
5937
|
+
ctx: childCtx,
|
|
5938
|
+
};
|
|
5939
|
+
})).then(handleResults);
|
|
5940
|
+
}
|
|
5941
|
+
else {
|
|
5942
|
+
let dirty = undefined;
|
|
5943
|
+
const issues = [];
|
|
5944
|
+
for (const option of options) {
|
|
5945
|
+
const childCtx = {
|
|
5946
|
+
...ctx,
|
|
5947
|
+
common: {
|
|
5948
|
+
...ctx.common,
|
|
5949
|
+
issues: [],
|
|
5950
|
+
},
|
|
5951
|
+
parent: null,
|
|
5952
|
+
};
|
|
5953
|
+
const result = option._parseSync({
|
|
5954
|
+
data: ctx.data,
|
|
5955
|
+
path: ctx.path,
|
|
5956
|
+
parent: childCtx,
|
|
5957
|
+
});
|
|
5958
|
+
if (result.status === "valid") {
|
|
5959
|
+
return result;
|
|
5960
|
+
}
|
|
5961
|
+
else if (result.status === "dirty" && !dirty) {
|
|
5962
|
+
dirty = { result, ctx: childCtx };
|
|
5963
|
+
}
|
|
5964
|
+
if (childCtx.common.issues.length) {
|
|
5965
|
+
issues.push(childCtx.common.issues);
|
|
5966
|
+
}
|
|
5967
|
+
}
|
|
5968
|
+
if (dirty) {
|
|
5969
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
5970
|
+
return dirty.result;
|
|
5971
|
+
}
|
|
5972
|
+
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
5973
|
+
addIssueToContext(ctx, {
|
|
5974
|
+
code: ZodIssueCode.invalid_union,
|
|
5975
|
+
unionErrors,
|
|
5976
|
+
});
|
|
5977
|
+
return INVALID;
|
|
5978
|
+
}
|
|
5979
|
+
}
|
|
5980
|
+
get options() {
|
|
5981
|
+
return this._def.options;
|
|
5982
|
+
}
|
|
5983
|
+
}
|
|
5984
|
+
ZodUnion.create = (types, params) => {
|
|
5985
|
+
return new ZodUnion({
|
|
5986
|
+
options: types,
|
|
5987
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
5988
|
+
...processCreateParams(params),
|
|
5989
|
+
});
|
|
5990
|
+
};
|
|
5991
|
+
function mergeValues(a, b) {
|
|
5992
|
+
const aType = getParsedType(a);
|
|
5993
|
+
const bType = getParsedType(b);
|
|
5994
|
+
if (a === b) {
|
|
5995
|
+
return { valid: true, data: a };
|
|
5996
|
+
}
|
|
5997
|
+
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
5998
|
+
const bKeys = util.objectKeys(b);
|
|
5999
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
6000
|
+
const newObj = { ...a, ...b };
|
|
6001
|
+
for (const key of sharedKeys) {
|
|
6002
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
6003
|
+
if (!sharedValue.valid) {
|
|
6004
|
+
return { valid: false };
|
|
6005
|
+
}
|
|
6006
|
+
newObj[key] = sharedValue.data;
|
|
6007
|
+
}
|
|
6008
|
+
return { valid: true, data: newObj };
|
|
6009
|
+
}
|
|
6010
|
+
else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
6011
|
+
if (a.length !== b.length) {
|
|
6012
|
+
return { valid: false };
|
|
6013
|
+
}
|
|
6014
|
+
const newArray = [];
|
|
6015
|
+
for (let index = 0; index < a.length; index++) {
|
|
6016
|
+
const itemA = a[index];
|
|
6017
|
+
const itemB = b[index];
|
|
6018
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
6019
|
+
if (!sharedValue.valid) {
|
|
6020
|
+
return { valid: false };
|
|
6021
|
+
}
|
|
6022
|
+
newArray.push(sharedValue.data);
|
|
6023
|
+
}
|
|
6024
|
+
return { valid: true, data: newArray };
|
|
6025
|
+
}
|
|
6026
|
+
else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
6027
|
+
return { valid: true, data: a };
|
|
6028
|
+
}
|
|
6029
|
+
else {
|
|
6030
|
+
return { valid: false };
|
|
6031
|
+
}
|
|
6032
|
+
}
|
|
6033
|
+
class ZodIntersection extends ZodType {
|
|
6034
|
+
_parse(input) {
|
|
6035
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6036
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
6037
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
6038
|
+
return INVALID;
|
|
6039
|
+
}
|
|
6040
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
6041
|
+
if (!merged.valid) {
|
|
6042
|
+
addIssueToContext(ctx, {
|
|
6043
|
+
code: ZodIssueCode.invalid_intersection_types,
|
|
6044
|
+
});
|
|
6045
|
+
return INVALID;
|
|
6046
|
+
}
|
|
6047
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
6048
|
+
status.dirty();
|
|
6049
|
+
}
|
|
6050
|
+
return { status: status.value, value: merged.data };
|
|
6051
|
+
};
|
|
6052
|
+
if (ctx.common.async) {
|
|
6053
|
+
return Promise.all([
|
|
6054
|
+
this._def.left._parseAsync({
|
|
6055
|
+
data: ctx.data,
|
|
6056
|
+
path: ctx.path,
|
|
6057
|
+
parent: ctx,
|
|
6058
|
+
}),
|
|
6059
|
+
this._def.right._parseAsync({
|
|
6060
|
+
data: ctx.data,
|
|
6061
|
+
path: ctx.path,
|
|
6062
|
+
parent: ctx,
|
|
6063
|
+
}),
|
|
6064
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
6065
|
+
}
|
|
6066
|
+
else {
|
|
6067
|
+
return handleParsed(this._def.left._parseSync({
|
|
6068
|
+
data: ctx.data,
|
|
6069
|
+
path: ctx.path,
|
|
6070
|
+
parent: ctx,
|
|
6071
|
+
}), this._def.right._parseSync({
|
|
6072
|
+
data: ctx.data,
|
|
6073
|
+
path: ctx.path,
|
|
6074
|
+
parent: ctx,
|
|
6075
|
+
}));
|
|
6076
|
+
}
|
|
6077
|
+
}
|
|
6078
|
+
}
|
|
6079
|
+
ZodIntersection.create = (left, right, params) => {
|
|
6080
|
+
return new ZodIntersection({
|
|
6081
|
+
left: left,
|
|
6082
|
+
right: right,
|
|
6083
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
6084
|
+
...processCreateParams(params),
|
|
6085
|
+
});
|
|
6086
|
+
};
|
|
6087
|
+
// type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]];
|
|
6088
|
+
class ZodTuple extends ZodType {
|
|
6089
|
+
_parse(input) {
|
|
6090
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6091
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
6092
|
+
addIssueToContext(ctx, {
|
|
6093
|
+
code: ZodIssueCode.invalid_type,
|
|
6094
|
+
expected: ZodParsedType.array,
|
|
6095
|
+
received: ctx.parsedType,
|
|
6096
|
+
});
|
|
6097
|
+
return INVALID;
|
|
6098
|
+
}
|
|
6099
|
+
if (ctx.data.length < this._def.items.length) {
|
|
6100
|
+
addIssueToContext(ctx, {
|
|
6101
|
+
code: ZodIssueCode.too_small,
|
|
6102
|
+
minimum: this._def.items.length,
|
|
6103
|
+
inclusive: true,
|
|
6104
|
+
exact: false,
|
|
6105
|
+
type: "array",
|
|
6106
|
+
});
|
|
6107
|
+
return INVALID;
|
|
6108
|
+
}
|
|
6109
|
+
const rest = this._def.rest;
|
|
6110
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
6111
|
+
addIssueToContext(ctx, {
|
|
6112
|
+
code: ZodIssueCode.too_big,
|
|
6113
|
+
maximum: this._def.items.length,
|
|
6114
|
+
inclusive: true,
|
|
6115
|
+
exact: false,
|
|
6116
|
+
type: "array",
|
|
6117
|
+
});
|
|
6118
|
+
status.dirty();
|
|
6119
|
+
}
|
|
6120
|
+
const items = [...ctx.data]
|
|
6121
|
+
.map((item, itemIndex) => {
|
|
6122
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
6123
|
+
if (!schema)
|
|
6124
|
+
return null;
|
|
6125
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
6126
|
+
})
|
|
6127
|
+
.filter((x) => !!x); // filter nulls
|
|
6128
|
+
if (ctx.common.async) {
|
|
6129
|
+
return Promise.all(items).then((results) => {
|
|
6130
|
+
return ParseStatus.mergeArray(status, results);
|
|
6131
|
+
});
|
|
6132
|
+
}
|
|
6133
|
+
else {
|
|
6134
|
+
return ParseStatus.mergeArray(status, items);
|
|
6135
|
+
}
|
|
6136
|
+
}
|
|
6137
|
+
get items() {
|
|
6138
|
+
return this._def.items;
|
|
6139
|
+
}
|
|
6140
|
+
rest(rest) {
|
|
6141
|
+
return new ZodTuple({
|
|
6142
|
+
...this._def,
|
|
6143
|
+
rest,
|
|
6144
|
+
});
|
|
6145
|
+
}
|
|
6146
|
+
}
|
|
6147
|
+
ZodTuple.create = (schemas, params) => {
|
|
6148
|
+
if (!Array.isArray(schemas)) {
|
|
6149
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
6150
|
+
}
|
|
6151
|
+
return new ZodTuple({
|
|
6152
|
+
items: schemas,
|
|
6153
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
6154
|
+
rest: null,
|
|
6155
|
+
...processCreateParams(params),
|
|
6156
|
+
});
|
|
6157
|
+
};
|
|
6158
|
+
class ZodRecord extends ZodType {
|
|
6159
|
+
get keySchema() {
|
|
6160
|
+
return this._def.keyType;
|
|
6161
|
+
}
|
|
6162
|
+
get valueSchema() {
|
|
6163
|
+
return this._def.valueType;
|
|
6164
|
+
}
|
|
6165
|
+
_parse(input) {
|
|
6166
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6167
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
6168
|
+
addIssueToContext(ctx, {
|
|
6169
|
+
code: ZodIssueCode.invalid_type,
|
|
6170
|
+
expected: ZodParsedType.object,
|
|
6171
|
+
received: ctx.parsedType,
|
|
6172
|
+
});
|
|
6173
|
+
return INVALID;
|
|
6174
|
+
}
|
|
6175
|
+
const pairs = [];
|
|
6176
|
+
const keyType = this._def.keyType;
|
|
6177
|
+
const valueType = this._def.valueType;
|
|
6178
|
+
for (const key in ctx.data) {
|
|
6179
|
+
pairs.push({
|
|
6180
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
6181
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
6182
|
+
alwaysSet: key in ctx.data,
|
|
6183
|
+
});
|
|
6184
|
+
}
|
|
6185
|
+
if (ctx.common.async) {
|
|
6186
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
6187
|
+
}
|
|
6188
|
+
else {
|
|
6189
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
6190
|
+
}
|
|
6191
|
+
}
|
|
6192
|
+
get element() {
|
|
6193
|
+
return this._def.valueType;
|
|
6194
|
+
}
|
|
6195
|
+
static create(first, second, third) {
|
|
6196
|
+
if (second instanceof ZodType) {
|
|
6197
|
+
return new ZodRecord({
|
|
6198
|
+
keyType: first,
|
|
6199
|
+
valueType: second,
|
|
6200
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
6201
|
+
...processCreateParams(third),
|
|
6202
|
+
});
|
|
6203
|
+
}
|
|
6204
|
+
return new ZodRecord({
|
|
6205
|
+
keyType: ZodString.create(),
|
|
6206
|
+
valueType: first,
|
|
6207
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
6208
|
+
...processCreateParams(second),
|
|
6209
|
+
});
|
|
6210
|
+
}
|
|
6211
|
+
}
|
|
6212
|
+
class ZodMap extends ZodType {
|
|
6213
|
+
get keySchema() {
|
|
6214
|
+
return this._def.keyType;
|
|
6215
|
+
}
|
|
6216
|
+
get valueSchema() {
|
|
6217
|
+
return this._def.valueType;
|
|
6218
|
+
}
|
|
6219
|
+
_parse(input) {
|
|
6220
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6221
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
6222
|
+
addIssueToContext(ctx, {
|
|
6223
|
+
code: ZodIssueCode.invalid_type,
|
|
6224
|
+
expected: ZodParsedType.map,
|
|
6225
|
+
received: ctx.parsedType,
|
|
6226
|
+
});
|
|
6227
|
+
return INVALID;
|
|
6228
|
+
}
|
|
6229
|
+
const keyType = this._def.keyType;
|
|
6230
|
+
const valueType = this._def.valueType;
|
|
6231
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
6232
|
+
return {
|
|
6233
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
6234
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
|
|
6235
|
+
};
|
|
6236
|
+
});
|
|
6237
|
+
if (ctx.common.async) {
|
|
6238
|
+
const finalMap = new Map();
|
|
6239
|
+
return Promise.resolve().then(async () => {
|
|
6240
|
+
for (const pair of pairs) {
|
|
6241
|
+
const key = await pair.key;
|
|
6242
|
+
const value = await pair.value;
|
|
6243
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
6244
|
+
return INVALID;
|
|
6245
|
+
}
|
|
6246
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
6247
|
+
status.dirty();
|
|
6248
|
+
}
|
|
6249
|
+
finalMap.set(key.value, value.value);
|
|
6250
|
+
}
|
|
6251
|
+
return { status: status.value, value: finalMap };
|
|
6252
|
+
});
|
|
6253
|
+
}
|
|
6254
|
+
else {
|
|
6255
|
+
const finalMap = new Map();
|
|
6256
|
+
for (const pair of pairs) {
|
|
6257
|
+
const key = pair.key;
|
|
6258
|
+
const value = pair.value;
|
|
6259
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
6260
|
+
return INVALID;
|
|
6261
|
+
}
|
|
6262
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
6263
|
+
status.dirty();
|
|
6264
|
+
}
|
|
6265
|
+
finalMap.set(key.value, value.value);
|
|
6266
|
+
}
|
|
6267
|
+
return { status: status.value, value: finalMap };
|
|
6268
|
+
}
|
|
6269
|
+
}
|
|
6270
|
+
}
|
|
6271
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
6272
|
+
return new ZodMap({
|
|
6273
|
+
valueType,
|
|
6274
|
+
keyType,
|
|
6275
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
6276
|
+
...processCreateParams(params),
|
|
6277
|
+
});
|
|
6278
|
+
};
|
|
6279
|
+
class ZodSet extends ZodType {
|
|
6280
|
+
_parse(input) {
|
|
6281
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6282
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
6283
|
+
addIssueToContext(ctx, {
|
|
6284
|
+
code: ZodIssueCode.invalid_type,
|
|
6285
|
+
expected: ZodParsedType.set,
|
|
6286
|
+
received: ctx.parsedType,
|
|
6287
|
+
});
|
|
6288
|
+
return INVALID;
|
|
6289
|
+
}
|
|
6290
|
+
const def = this._def;
|
|
6291
|
+
if (def.minSize !== null) {
|
|
6292
|
+
if (ctx.data.size < def.minSize.value) {
|
|
6293
|
+
addIssueToContext(ctx, {
|
|
6294
|
+
code: ZodIssueCode.too_small,
|
|
6295
|
+
minimum: def.minSize.value,
|
|
6296
|
+
type: "set",
|
|
6297
|
+
inclusive: true,
|
|
6298
|
+
exact: false,
|
|
6299
|
+
message: def.minSize.message,
|
|
6300
|
+
});
|
|
6301
|
+
status.dirty();
|
|
6302
|
+
}
|
|
6303
|
+
}
|
|
6304
|
+
if (def.maxSize !== null) {
|
|
6305
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
6306
|
+
addIssueToContext(ctx, {
|
|
6307
|
+
code: ZodIssueCode.too_big,
|
|
6308
|
+
maximum: def.maxSize.value,
|
|
6309
|
+
type: "set",
|
|
6310
|
+
inclusive: true,
|
|
6311
|
+
exact: false,
|
|
6312
|
+
message: def.maxSize.message,
|
|
6313
|
+
});
|
|
6314
|
+
status.dirty();
|
|
6315
|
+
}
|
|
6316
|
+
}
|
|
6317
|
+
const valueType = this._def.valueType;
|
|
6318
|
+
function finalizeSet(elements) {
|
|
6319
|
+
const parsedSet = new Set();
|
|
6320
|
+
for (const element of elements) {
|
|
6321
|
+
if (element.status === "aborted")
|
|
6322
|
+
return INVALID;
|
|
6323
|
+
if (element.status === "dirty")
|
|
6324
|
+
status.dirty();
|
|
6325
|
+
parsedSet.add(element.value);
|
|
6326
|
+
}
|
|
6327
|
+
return { status: status.value, value: parsedSet };
|
|
6328
|
+
}
|
|
6329
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
6330
|
+
if (ctx.common.async) {
|
|
6331
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
6332
|
+
}
|
|
6333
|
+
else {
|
|
6334
|
+
return finalizeSet(elements);
|
|
6335
|
+
}
|
|
6336
|
+
}
|
|
6337
|
+
min(minSize, message) {
|
|
6338
|
+
return new ZodSet({
|
|
6339
|
+
...this._def,
|
|
6340
|
+
minSize: { value: minSize, message: errorUtil.toString(message) },
|
|
6341
|
+
});
|
|
6342
|
+
}
|
|
6343
|
+
max(maxSize, message) {
|
|
6344
|
+
return new ZodSet({
|
|
6345
|
+
...this._def,
|
|
6346
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) },
|
|
6347
|
+
});
|
|
6348
|
+
}
|
|
6349
|
+
size(size, message) {
|
|
6350
|
+
return this.min(size, message).max(size, message);
|
|
6351
|
+
}
|
|
6352
|
+
nonempty(message) {
|
|
6353
|
+
return this.min(1, message);
|
|
6354
|
+
}
|
|
6355
|
+
}
|
|
6356
|
+
ZodSet.create = (valueType, params) => {
|
|
6357
|
+
return new ZodSet({
|
|
6358
|
+
valueType,
|
|
6359
|
+
minSize: null,
|
|
6360
|
+
maxSize: null,
|
|
6361
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
6362
|
+
...processCreateParams(params),
|
|
6363
|
+
});
|
|
6364
|
+
};
|
|
6365
|
+
class ZodLazy extends ZodType {
|
|
6366
|
+
get schema() {
|
|
6367
|
+
return this._def.getter();
|
|
6368
|
+
}
|
|
6369
|
+
_parse(input) {
|
|
6370
|
+
const { ctx } = this._processInputParams(input);
|
|
6371
|
+
const lazySchema = this._def.getter();
|
|
6372
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
6373
|
+
}
|
|
6374
|
+
}
|
|
6375
|
+
ZodLazy.create = (getter, params) => {
|
|
6376
|
+
return new ZodLazy({
|
|
6377
|
+
getter: getter,
|
|
6378
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
6379
|
+
...processCreateParams(params),
|
|
6380
|
+
});
|
|
6381
|
+
};
|
|
6382
|
+
class ZodLiteral extends ZodType {
|
|
6383
|
+
_parse(input) {
|
|
6384
|
+
if (input.data !== this._def.value) {
|
|
6385
|
+
const ctx = this._getOrReturnCtx(input);
|
|
6386
|
+
addIssueToContext(ctx, {
|
|
6387
|
+
received: ctx.data,
|
|
6388
|
+
code: ZodIssueCode.invalid_literal,
|
|
6389
|
+
expected: this._def.value,
|
|
6390
|
+
});
|
|
6391
|
+
return INVALID;
|
|
6392
|
+
}
|
|
6393
|
+
return { status: "valid", value: input.data };
|
|
6394
|
+
}
|
|
6395
|
+
get value() {
|
|
6396
|
+
return this._def.value;
|
|
6397
|
+
}
|
|
6398
|
+
}
|
|
6399
|
+
ZodLiteral.create = (value, params) => {
|
|
6400
|
+
return new ZodLiteral({
|
|
6401
|
+
value: value,
|
|
6402
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
6403
|
+
...processCreateParams(params),
|
|
6404
|
+
});
|
|
6405
|
+
};
|
|
6406
|
+
function createZodEnum(values, params) {
|
|
6407
|
+
return new ZodEnum({
|
|
6408
|
+
values,
|
|
6409
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
6410
|
+
...processCreateParams(params),
|
|
6411
|
+
});
|
|
6412
|
+
}
|
|
6413
|
+
class ZodEnum extends ZodType {
|
|
6414
|
+
_parse(input) {
|
|
6415
|
+
if (typeof input.data !== "string") {
|
|
6416
|
+
const ctx = this._getOrReturnCtx(input);
|
|
6417
|
+
const expectedValues = this._def.values;
|
|
6418
|
+
addIssueToContext(ctx, {
|
|
6419
|
+
expected: util.joinValues(expectedValues),
|
|
6420
|
+
received: ctx.parsedType,
|
|
6421
|
+
code: ZodIssueCode.invalid_type,
|
|
6422
|
+
});
|
|
6423
|
+
return INVALID;
|
|
6424
|
+
}
|
|
6425
|
+
if (!this._cache) {
|
|
6426
|
+
this._cache = new Set(this._def.values);
|
|
6427
|
+
}
|
|
6428
|
+
if (!this._cache.has(input.data)) {
|
|
6429
|
+
const ctx = this._getOrReturnCtx(input);
|
|
6430
|
+
const expectedValues = this._def.values;
|
|
6431
|
+
addIssueToContext(ctx, {
|
|
6432
|
+
received: ctx.data,
|
|
6433
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
6434
|
+
options: expectedValues,
|
|
6435
|
+
});
|
|
6436
|
+
return INVALID;
|
|
6437
|
+
}
|
|
6438
|
+
return OK(input.data);
|
|
6439
|
+
}
|
|
6440
|
+
get options() {
|
|
6441
|
+
return this._def.values;
|
|
6442
|
+
}
|
|
6443
|
+
get enum() {
|
|
6444
|
+
const enumValues = {};
|
|
6445
|
+
for (const val of this._def.values) {
|
|
6446
|
+
enumValues[val] = val;
|
|
6447
|
+
}
|
|
6448
|
+
return enumValues;
|
|
6449
|
+
}
|
|
6450
|
+
get Values() {
|
|
6451
|
+
const enumValues = {};
|
|
6452
|
+
for (const val of this._def.values) {
|
|
6453
|
+
enumValues[val] = val;
|
|
6454
|
+
}
|
|
6455
|
+
return enumValues;
|
|
6456
|
+
}
|
|
6457
|
+
get Enum() {
|
|
6458
|
+
const enumValues = {};
|
|
6459
|
+
for (const val of this._def.values) {
|
|
6460
|
+
enumValues[val] = val;
|
|
6461
|
+
}
|
|
6462
|
+
return enumValues;
|
|
6463
|
+
}
|
|
6464
|
+
extract(values, newDef = this._def) {
|
|
6465
|
+
return ZodEnum.create(values, {
|
|
6466
|
+
...this._def,
|
|
6467
|
+
...newDef,
|
|
6468
|
+
});
|
|
6469
|
+
}
|
|
6470
|
+
exclude(values, newDef = this._def) {
|
|
6471
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
6472
|
+
...this._def,
|
|
6473
|
+
...newDef,
|
|
6474
|
+
});
|
|
6475
|
+
}
|
|
6476
|
+
}
|
|
6477
|
+
ZodEnum.create = createZodEnum;
|
|
6478
|
+
class ZodNativeEnum extends ZodType {
|
|
6479
|
+
_parse(input) {
|
|
6480
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
6481
|
+
const ctx = this._getOrReturnCtx(input);
|
|
6482
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
6483
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
6484
|
+
addIssueToContext(ctx, {
|
|
6485
|
+
expected: util.joinValues(expectedValues),
|
|
6486
|
+
received: ctx.parsedType,
|
|
6487
|
+
code: ZodIssueCode.invalid_type,
|
|
6488
|
+
});
|
|
6489
|
+
return INVALID;
|
|
6490
|
+
}
|
|
6491
|
+
if (!this._cache) {
|
|
6492
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
6493
|
+
}
|
|
6494
|
+
if (!this._cache.has(input.data)) {
|
|
6495
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
6496
|
+
addIssueToContext(ctx, {
|
|
6497
|
+
received: ctx.data,
|
|
6498
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
6499
|
+
options: expectedValues,
|
|
6500
|
+
});
|
|
6501
|
+
return INVALID;
|
|
6502
|
+
}
|
|
6503
|
+
return OK(input.data);
|
|
6504
|
+
}
|
|
6505
|
+
get enum() {
|
|
6506
|
+
return this._def.values;
|
|
6507
|
+
}
|
|
6508
|
+
}
|
|
6509
|
+
ZodNativeEnum.create = (values, params) => {
|
|
6510
|
+
return new ZodNativeEnum({
|
|
6511
|
+
values: values,
|
|
6512
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
6513
|
+
...processCreateParams(params),
|
|
6514
|
+
});
|
|
6515
|
+
};
|
|
6516
|
+
class ZodPromise extends ZodType {
|
|
6517
|
+
unwrap() {
|
|
6518
|
+
return this._def.type;
|
|
6519
|
+
}
|
|
6520
|
+
_parse(input) {
|
|
6521
|
+
const { ctx } = this._processInputParams(input);
|
|
6522
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
6523
|
+
addIssueToContext(ctx, {
|
|
6524
|
+
code: ZodIssueCode.invalid_type,
|
|
6525
|
+
expected: ZodParsedType.promise,
|
|
6526
|
+
received: ctx.parsedType,
|
|
6527
|
+
});
|
|
6528
|
+
return INVALID;
|
|
6529
|
+
}
|
|
6530
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
6531
|
+
return OK(promisified.then((data) => {
|
|
6532
|
+
return this._def.type.parseAsync(data, {
|
|
6533
|
+
path: ctx.path,
|
|
6534
|
+
errorMap: ctx.common.contextualErrorMap,
|
|
6535
|
+
});
|
|
6536
|
+
}));
|
|
6537
|
+
}
|
|
6538
|
+
}
|
|
6539
|
+
ZodPromise.create = (schema, params) => {
|
|
6540
|
+
return new ZodPromise({
|
|
6541
|
+
type: schema,
|
|
6542
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
6543
|
+
...processCreateParams(params),
|
|
6544
|
+
});
|
|
6545
|
+
};
|
|
6546
|
+
class ZodEffects extends ZodType {
|
|
6547
|
+
innerType() {
|
|
6548
|
+
return this._def.schema;
|
|
6549
|
+
}
|
|
6550
|
+
sourceType() {
|
|
6551
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects
|
|
6552
|
+
? this._def.schema.sourceType()
|
|
6553
|
+
: this._def.schema;
|
|
6554
|
+
}
|
|
6555
|
+
_parse(input) {
|
|
6556
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6557
|
+
const effect = this._def.effect || null;
|
|
6558
|
+
const checkCtx = {
|
|
6559
|
+
addIssue: (arg) => {
|
|
6560
|
+
addIssueToContext(ctx, arg);
|
|
6561
|
+
if (arg.fatal) {
|
|
6562
|
+
status.abort();
|
|
6563
|
+
}
|
|
6564
|
+
else {
|
|
6565
|
+
status.dirty();
|
|
6566
|
+
}
|
|
6567
|
+
},
|
|
6568
|
+
get path() {
|
|
6569
|
+
return ctx.path;
|
|
6570
|
+
},
|
|
6571
|
+
};
|
|
6572
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
6573
|
+
if (effect.type === "preprocess") {
|
|
6574
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
6575
|
+
if (ctx.common.async) {
|
|
6576
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
6577
|
+
if (status.value === "aborted")
|
|
6578
|
+
return INVALID;
|
|
6579
|
+
const result = await this._def.schema._parseAsync({
|
|
6580
|
+
data: processed,
|
|
6581
|
+
path: ctx.path,
|
|
6582
|
+
parent: ctx,
|
|
6583
|
+
});
|
|
6584
|
+
if (result.status === "aborted")
|
|
6585
|
+
return INVALID;
|
|
6586
|
+
if (result.status === "dirty")
|
|
6587
|
+
return DIRTY(result.value);
|
|
6588
|
+
if (status.value === "dirty")
|
|
6589
|
+
return DIRTY(result.value);
|
|
6590
|
+
return result;
|
|
6591
|
+
});
|
|
6592
|
+
}
|
|
6593
|
+
else {
|
|
6594
|
+
if (status.value === "aborted")
|
|
6595
|
+
return INVALID;
|
|
6596
|
+
const result = this._def.schema._parseSync({
|
|
6597
|
+
data: processed,
|
|
6598
|
+
path: ctx.path,
|
|
6599
|
+
parent: ctx,
|
|
6600
|
+
});
|
|
6601
|
+
if (result.status === "aborted")
|
|
6602
|
+
return INVALID;
|
|
6603
|
+
if (result.status === "dirty")
|
|
6604
|
+
return DIRTY(result.value);
|
|
6605
|
+
if (status.value === "dirty")
|
|
6606
|
+
return DIRTY(result.value);
|
|
6607
|
+
return result;
|
|
6608
|
+
}
|
|
6609
|
+
}
|
|
6610
|
+
if (effect.type === "refinement") {
|
|
6611
|
+
const executeRefinement = (acc) => {
|
|
6612
|
+
const result = effect.refinement(acc, checkCtx);
|
|
6613
|
+
if (ctx.common.async) {
|
|
6614
|
+
return Promise.resolve(result);
|
|
6615
|
+
}
|
|
6616
|
+
if (result instanceof Promise) {
|
|
6617
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
6618
|
+
}
|
|
6619
|
+
return acc;
|
|
6620
|
+
};
|
|
6621
|
+
if (ctx.common.async === false) {
|
|
6622
|
+
const inner = this._def.schema._parseSync({
|
|
6623
|
+
data: ctx.data,
|
|
6624
|
+
path: ctx.path,
|
|
6625
|
+
parent: ctx,
|
|
6626
|
+
});
|
|
6627
|
+
if (inner.status === "aborted")
|
|
6628
|
+
return INVALID;
|
|
6629
|
+
if (inner.status === "dirty")
|
|
6630
|
+
status.dirty();
|
|
6631
|
+
// return value is ignored
|
|
6632
|
+
executeRefinement(inner.value);
|
|
6633
|
+
return { status: status.value, value: inner.value };
|
|
6634
|
+
}
|
|
6635
|
+
else {
|
|
6636
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
6637
|
+
if (inner.status === "aborted")
|
|
6638
|
+
return INVALID;
|
|
6639
|
+
if (inner.status === "dirty")
|
|
6640
|
+
status.dirty();
|
|
6641
|
+
return executeRefinement(inner.value).then(() => {
|
|
6642
|
+
return { status: status.value, value: inner.value };
|
|
6643
|
+
});
|
|
6644
|
+
});
|
|
6645
|
+
}
|
|
6646
|
+
}
|
|
6647
|
+
if (effect.type === "transform") {
|
|
6648
|
+
if (ctx.common.async === false) {
|
|
6649
|
+
const base = this._def.schema._parseSync({
|
|
6650
|
+
data: ctx.data,
|
|
6651
|
+
path: ctx.path,
|
|
6652
|
+
parent: ctx,
|
|
6653
|
+
});
|
|
6654
|
+
if (!isValid(base))
|
|
6655
|
+
return INVALID;
|
|
6656
|
+
const result = effect.transform(base.value, checkCtx);
|
|
6657
|
+
if (result instanceof Promise) {
|
|
6658
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
6659
|
+
}
|
|
6660
|
+
return { status: status.value, value: result };
|
|
6661
|
+
}
|
|
6662
|
+
else {
|
|
6663
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
6664
|
+
if (!isValid(base))
|
|
6665
|
+
return INVALID;
|
|
6666
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
6667
|
+
status: status.value,
|
|
6668
|
+
value: result,
|
|
6669
|
+
}));
|
|
6670
|
+
});
|
|
6671
|
+
}
|
|
6672
|
+
}
|
|
6673
|
+
util.assertNever(effect);
|
|
6674
|
+
}
|
|
6675
|
+
}
|
|
6676
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
6677
|
+
return new ZodEffects({
|
|
6678
|
+
schema,
|
|
6679
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
6680
|
+
effect,
|
|
6681
|
+
...processCreateParams(params),
|
|
6682
|
+
});
|
|
6683
|
+
};
|
|
6684
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
6685
|
+
return new ZodEffects({
|
|
6686
|
+
schema,
|
|
6687
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
6688
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
6689
|
+
...processCreateParams(params),
|
|
6690
|
+
});
|
|
6691
|
+
};
|
|
6692
|
+
class ZodOptional extends ZodType {
|
|
6693
|
+
_parse(input) {
|
|
6694
|
+
const parsedType = this._getType(input);
|
|
6695
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
6696
|
+
return OK(undefined);
|
|
6697
|
+
}
|
|
6698
|
+
return this._def.innerType._parse(input);
|
|
6699
|
+
}
|
|
6700
|
+
unwrap() {
|
|
6701
|
+
return this._def.innerType;
|
|
6702
|
+
}
|
|
6703
|
+
}
|
|
6704
|
+
ZodOptional.create = (type, params) => {
|
|
6705
|
+
return new ZodOptional({
|
|
6706
|
+
innerType: type,
|
|
6707
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
6708
|
+
...processCreateParams(params),
|
|
6709
|
+
});
|
|
6710
|
+
};
|
|
6711
|
+
class ZodNullable extends ZodType {
|
|
6712
|
+
_parse(input) {
|
|
6713
|
+
const parsedType = this._getType(input);
|
|
6714
|
+
if (parsedType === ZodParsedType.null) {
|
|
6715
|
+
return OK(null);
|
|
6716
|
+
}
|
|
6717
|
+
return this._def.innerType._parse(input);
|
|
6718
|
+
}
|
|
6719
|
+
unwrap() {
|
|
6720
|
+
return this._def.innerType;
|
|
6721
|
+
}
|
|
6722
|
+
}
|
|
6723
|
+
ZodNullable.create = (type, params) => {
|
|
6724
|
+
return new ZodNullable({
|
|
6725
|
+
innerType: type,
|
|
6726
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
6727
|
+
...processCreateParams(params),
|
|
6728
|
+
});
|
|
6729
|
+
};
|
|
6730
|
+
class ZodDefault extends ZodType {
|
|
6731
|
+
_parse(input) {
|
|
6732
|
+
const { ctx } = this._processInputParams(input);
|
|
6733
|
+
let data = ctx.data;
|
|
6734
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
6735
|
+
data = this._def.defaultValue();
|
|
6736
|
+
}
|
|
6737
|
+
return this._def.innerType._parse({
|
|
6738
|
+
data,
|
|
6739
|
+
path: ctx.path,
|
|
6740
|
+
parent: ctx,
|
|
6741
|
+
});
|
|
6742
|
+
}
|
|
6743
|
+
removeDefault() {
|
|
6744
|
+
return this._def.innerType;
|
|
6745
|
+
}
|
|
6746
|
+
}
|
|
6747
|
+
ZodDefault.create = (type, params) => {
|
|
6748
|
+
return new ZodDefault({
|
|
6749
|
+
innerType: type,
|
|
6750
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
6751
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
6752
|
+
...processCreateParams(params),
|
|
6753
|
+
});
|
|
6754
|
+
};
|
|
6755
|
+
class ZodCatch extends ZodType {
|
|
6756
|
+
_parse(input) {
|
|
6757
|
+
const { ctx } = this._processInputParams(input);
|
|
6758
|
+
// newCtx is used to not collect issues from inner types in ctx
|
|
6759
|
+
const newCtx = {
|
|
6760
|
+
...ctx,
|
|
6761
|
+
common: {
|
|
6762
|
+
...ctx.common,
|
|
6763
|
+
issues: [],
|
|
6764
|
+
},
|
|
6765
|
+
};
|
|
6766
|
+
const result = this._def.innerType._parse({
|
|
6767
|
+
data: newCtx.data,
|
|
6768
|
+
path: newCtx.path,
|
|
6769
|
+
parent: {
|
|
6770
|
+
...newCtx,
|
|
6771
|
+
},
|
|
6772
|
+
});
|
|
6773
|
+
if (isAsync(result)) {
|
|
6774
|
+
return result.then((result) => {
|
|
6775
|
+
return {
|
|
6776
|
+
status: "valid",
|
|
6777
|
+
value: result.status === "valid"
|
|
6778
|
+
? result.value
|
|
6779
|
+
: this._def.catchValue({
|
|
6780
|
+
get error() {
|
|
6781
|
+
return new ZodError(newCtx.common.issues);
|
|
6782
|
+
},
|
|
6783
|
+
input: newCtx.data,
|
|
6784
|
+
}),
|
|
6785
|
+
};
|
|
6786
|
+
});
|
|
6787
|
+
}
|
|
6788
|
+
else {
|
|
6789
|
+
return {
|
|
6790
|
+
status: "valid",
|
|
6791
|
+
value: result.status === "valid"
|
|
6792
|
+
? result.value
|
|
6793
|
+
: this._def.catchValue({
|
|
6794
|
+
get error() {
|
|
6795
|
+
return new ZodError(newCtx.common.issues);
|
|
6796
|
+
},
|
|
6797
|
+
input: newCtx.data,
|
|
6798
|
+
}),
|
|
6799
|
+
};
|
|
6800
|
+
}
|
|
6801
|
+
}
|
|
6802
|
+
removeCatch() {
|
|
6803
|
+
return this._def.innerType;
|
|
6804
|
+
}
|
|
6805
|
+
}
|
|
6806
|
+
ZodCatch.create = (type, params) => {
|
|
6807
|
+
return new ZodCatch({
|
|
6808
|
+
innerType: type,
|
|
6809
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
6810
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
6811
|
+
...processCreateParams(params),
|
|
6812
|
+
});
|
|
6813
|
+
};
|
|
6814
|
+
class ZodNaN extends ZodType {
|
|
6815
|
+
_parse(input) {
|
|
6816
|
+
const parsedType = this._getType(input);
|
|
6817
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
6818
|
+
const ctx = this._getOrReturnCtx(input);
|
|
6819
|
+
addIssueToContext(ctx, {
|
|
6820
|
+
code: ZodIssueCode.invalid_type,
|
|
6821
|
+
expected: ZodParsedType.nan,
|
|
6822
|
+
received: ctx.parsedType,
|
|
6823
|
+
});
|
|
6824
|
+
return INVALID;
|
|
6825
|
+
}
|
|
6826
|
+
return { status: "valid", value: input.data };
|
|
6827
|
+
}
|
|
6828
|
+
}
|
|
6829
|
+
ZodNaN.create = (params) => {
|
|
6830
|
+
return new ZodNaN({
|
|
6831
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
6832
|
+
...processCreateParams(params),
|
|
6833
|
+
});
|
|
6834
|
+
};
|
|
6835
|
+
class ZodBranded extends ZodType {
|
|
6836
|
+
_parse(input) {
|
|
6837
|
+
const { ctx } = this._processInputParams(input);
|
|
6838
|
+
const data = ctx.data;
|
|
6839
|
+
return this._def.type._parse({
|
|
6840
|
+
data,
|
|
6841
|
+
path: ctx.path,
|
|
6842
|
+
parent: ctx,
|
|
6843
|
+
});
|
|
6844
|
+
}
|
|
6845
|
+
unwrap() {
|
|
6846
|
+
return this._def.type;
|
|
6847
|
+
}
|
|
6848
|
+
}
|
|
6849
|
+
class ZodPipeline extends ZodType {
|
|
6850
|
+
_parse(input) {
|
|
6851
|
+
const { status, ctx } = this._processInputParams(input);
|
|
6852
|
+
if (ctx.common.async) {
|
|
6853
|
+
const handleAsync = async () => {
|
|
6854
|
+
const inResult = await this._def.in._parseAsync({
|
|
6855
|
+
data: ctx.data,
|
|
6856
|
+
path: ctx.path,
|
|
6857
|
+
parent: ctx,
|
|
6858
|
+
});
|
|
6859
|
+
if (inResult.status === "aborted")
|
|
6860
|
+
return INVALID;
|
|
6861
|
+
if (inResult.status === "dirty") {
|
|
6862
|
+
status.dirty();
|
|
6863
|
+
return DIRTY(inResult.value);
|
|
6864
|
+
}
|
|
6865
|
+
else {
|
|
6866
|
+
return this._def.out._parseAsync({
|
|
6867
|
+
data: inResult.value,
|
|
6868
|
+
path: ctx.path,
|
|
6869
|
+
parent: ctx,
|
|
6870
|
+
});
|
|
6871
|
+
}
|
|
6872
|
+
};
|
|
6873
|
+
return handleAsync();
|
|
6874
|
+
}
|
|
6875
|
+
else {
|
|
6876
|
+
const inResult = this._def.in._parseSync({
|
|
6877
|
+
data: ctx.data,
|
|
6878
|
+
path: ctx.path,
|
|
6879
|
+
parent: ctx,
|
|
6880
|
+
});
|
|
6881
|
+
if (inResult.status === "aborted")
|
|
6882
|
+
return INVALID;
|
|
6883
|
+
if (inResult.status === "dirty") {
|
|
6884
|
+
status.dirty();
|
|
6885
|
+
return {
|
|
6886
|
+
status: "dirty",
|
|
6887
|
+
value: inResult.value,
|
|
6888
|
+
};
|
|
6889
|
+
}
|
|
6890
|
+
else {
|
|
6891
|
+
return this._def.out._parseSync({
|
|
6892
|
+
data: inResult.value,
|
|
6893
|
+
path: ctx.path,
|
|
6894
|
+
parent: ctx,
|
|
6895
|
+
});
|
|
6896
|
+
}
|
|
6897
|
+
}
|
|
6898
|
+
}
|
|
6899
|
+
static create(a, b) {
|
|
6900
|
+
return new ZodPipeline({
|
|
6901
|
+
in: a,
|
|
6902
|
+
out: b,
|
|
6903
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline,
|
|
6904
|
+
});
|
|
6905
|
+
}
|
|
6906
|
+
}
|
|
6907
|
+
class ZodReadonly extends ZodType {
|
|
6908
|
+
_parse(input) {
|
|
6909
|
+
const result = this._def.innerType._parse(input);
|
|
6910
|
+
const freeze = (data) => {
|
|
6911
|
+
if (isValid(data)) {
|
|
6912
|
+
data.value = Object.freeze(data.value);
|
|
6913
|
+
}
|
|
6914
|
+
return data;
|
|
6915
|
+
};
|
|
6916
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
6917
|
+
}
|
|
6918
|
+
unwrap() {
|
|
6919
|
+
return this._def.innerType;
|
|
6920
|
+
}
|
|
6921
|
+
}
|
|
6922
|
+
ZodReadonly.create = (type, params) => {
|
|
6923
|
+
return new ZodReadonly({
|
|
6924
|
+
innerType: type,
|
|
6925
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
6926
|
+
...processCreateParams(params),
|
|
6927
|
+
});
|
|
6928
|
+
};
|
|
6929
|
+
var ZodFirstPartyTypeKind;
|
|
6930
|
+
(function (ZodFirstPartyTypeKind) {
|
|
6931
|
+
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
6932
|
+
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
6933
|
+
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
6934
|
+
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
6935
|
+
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
6936
|
+
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
6937
|
+
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
6938
|
+
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
6939
|
+
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
6940
|
+
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
6941
|
+
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
6942
|
+
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
6943
|
+
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
6944
|
+
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
6945
|
+
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
6946
|
+
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
6947
|
+
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
6948
|
+
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
6949
|
+
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
6950
|
+
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
6951
|
+
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
6952
|
+
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
6953
|
+
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
6954
|
+
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
6955
|
+
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
6956
|
+
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
6957
|
+
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
6958
|
+
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
6959
|
+
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
6960
|
+
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
6961
|
+
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
6962
|
+
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
6963
|
+
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
6964
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
6965
|
+
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
6966
|
+
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
6967
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
6968
|
+
const stringType = ZodString.create;
|
|
6969
|
+
const numberType = ZodNumber.create;
|
|
6970
|
+
const booleanType = ZodBoolean.create;
|
|
6971
|
+
ZodNever.create;
|
|
6972
|
+
const arrayType = ZodArray.create;
|
|
6973
|
+
const objectType = ZodObject.create;
|
|
6974
|
+
ZodUnion.create;
|
|
6975
|
+
ZodIntersection.create;
|
|
6976
|
+
ZodTuple.create;
|
|
6977
|
+
const recordType = ZodRecord.create;
|
|
6978
|
+
const enumType = ZodEnum.create;
|
|
6979
|
+
const nativeEnumType = ZodNativeEnum.create;
|
|
6980
|
+
ZodPromise.create;
|
|
6981
|
+
ZodOptional.create;
|
|
6982
|
+
ZodNullable.create;
|
|
6983
|
+
|
|
6984
|
+
/**
|
|
6985
|
+
* Schema for conditions that require explicit human approval before execution.
|
|
6986
|
+
* Each flag controls whether a specific trade type needs manual sign-off.
|
|
6987
|
+
*/
|
|
6988
|
+
const RequireHumanApprovalSchema = objectType({
|
|
6989
|
+
firstTradeInSymbol: booleanType().default(false),
|
|
6990
|
+
reversals: booleanType().default(false),
|
|
6991
|
+
shortSales: booleanType().default(true),
|
|
6992
|
+
leverageIncreases: booleanType().default(true),
|
|
6993
|
+
overnightHolds: booleanType().default(false),
|
|
6994
|
+
afterHoursTrades: booleanType().default(false),
|
|
6995
|
+
cryptoTrades: booleanType().default(false),
|
|
6996
|
+
concentratedPositions: booleanType().default(true),
|
|
6997
|
+
largeNotionalOrders: booleanType().default(true),
|
|
6998
|
+
largeNotionalThreshold: numberType().min(0).default(50000),
|
|
6999
|
+
portfolioLiquidation: booleanType().default(true),
|
|
7000
|
+
closeAllOrdersAndPositions: booleanType().default(true),
|
|
7001
|
+
policyMutations: booleanType().default(true),
|
|
7002
|
+
advancedModelEscalations: booleanType().default(false),
|
|
7003
|
+
})
|
|
7004
|
+
.default({});
|
|
7005
|
+
/**
|
|
7006
|
+
* Schema for allowed trading session windows.
|
|
7007
|
+
* Controls which market sessions the system may trade during.
|
|
7008
|
+
*/
|
|
7009
|
+
const AllowedSessionsSchema = objectType({
|
|
7010
|
+
premarket: booleanType().default(false),
|
|
7011
|
+
regular: booleanType().default(true),
|
|
7012
|
+
afterHours: booleanType().default(false),
|
|
7013
|
+
overnight: booleanType().default(false),
|
|
7014
|
+
weekends: booleanType().default(false),
|
|
7015
|
+
})
|
|
7016
|
+
.default({});
|
|
7017
|
+
/**
|
|
7018
|
+
* Autonomy preferences schema (section 7.1).
|
|
7019
|
+
* Governs the level of automated decision-making, human approval gates,
|
|
7020
|
+
* auto-pause triggers, and allowed trading sessions.
|
|
7021
|
+
*
|
|
7022
|
+
* The raw ZodObject variant (`AutonomyPrefsObjectSchema`) is exported
|
|
7023
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7024
|
+
*/
|
|
7025
|
+
const AutonomyPrefsObjectSchema = objectType({
|
|
7026
|
+
requireHumanApprovalFor: RequireHumanApprovalSchema,
|
|
7027
|
+
autoPauseOnIncident: booleanType().default(true),
|
|
7028
|
+
autoPauseOnBlackSwan: booleanType().default(true),
|
|
7029
|
+
autoPauseOnBrokerDegradation: booleanType().default(true),
|
|
7030
|
+
autoPauseOnDataQualityIssues: booleanType().default(true),
|
|
7031
|
+
autoPauseOnExcessSlippage: booleanType().default(false),
|
|
7032
|
+
excessSlippageThresholdPct: numberType().min(0).max(100).default(2),
|
|
7033
|
+
autoPauseOnDrawdownBreach: booleanType().default(true),
|
|
7034
|
+
autoPauseOnModelConfidenceCollapse: booleanType().default(false),
|
|
7035
|
+
modelConfidenceCollapseThreshold: numberType().min(0).max(100).default(30),
|
|
7036
|
+
allowedSessions: AllowedSessionsSchema,
|
|
7037
|
+
});
|
|
7038
|
+
const AutonomyPrefsSchema = AutonomyPrefsObjectSchema.default({});
|
|
7039
|
+
|
|
7040
|
+
const DirectionSchema = enumType(["long_only", "long_short", "market_neutral"]);
|
|
7041
|
+
/**
|
|
7042
|
+
* Asset universe preferences schema (section 7.2).
|
|
7043
|
+
* Defines which instruments, exchanges, sectors, and market segments
|
|
7044
|
+
* the account is permitted to trade, along with liquidity and eligibility filters.
|
|
7045
|
+
*
|
|
7046
|
+
* The raw ZodObject variant (`AssetUniversePrefsObjectSchema`) is exported
|
|
7047
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7048
|
+
*/
|
|
7049
|
+
const AssetUniversePrefsObjectSchema = objectType({
|
|
7050
|
+
equitiesDirection: DirectionSchema.default("long_only"),
|
|
7051
|
+
etfsDirection: enumType(["long_only", "long_short"]).default("long_only"),
|
|
7052
|
+
cryptoDirection: enumType(["long_only", "long_short"]).default("long_only"),
|
|
7053
|
+
optionsDirection: enumType(["long_only", "long_short"]).default("long_only"),
|
|
7054
|
+
allowedExchanges: arrayType(stringType()).default([]),
|
|
7055
|
+
deniedExchanges: arrayType(stringType()).default([]),
|
|
7056
|
+
allowedCountries: arrayType(stringType()).default([]),
|
|
7057
|
+
deniedCountries: arrayType(stringType()).default([]),
|
|
7058
|
+
allowedSectors: arrayType(stringType()).default([]),
|
|
7059
|
+
deniedSectors: arrayType(stringType()).default([]),
|
|
7060
|
+
allowedSymbols: arrayType(stringType()).default([]),
|
|
7061
|
+
deniedSymbols: arrayType(stringType()).default([]),
|
|
7062
|
+
cryptoSpotOnly: booleanType().default(true),
|
|
7063
|
+
allowedCryptoPairs: arrayType(stringType()).default([]),
|
|
7064
|
+
deniedCryptoPairs: arrayType(stringType()).default([]),
|
|
7065
|
+
minMarketCapMillions: numberType().min(0).default(0),
|
|
7066
|
+
minAvgDailyVolume: numberType().min(0).default(0),
|
|
7067
|
+
minPrice: numberType().min(0).default(0),
|
|
7068
|
+
maxPrice: numberType().min(0).default(0),
|
|
7069
|
+
maxSpreadPct: numberType().min(0).default(0),
|
|
7070
|
+
minLiquidityScore: numberType().min(0).max(100).default(0),
|
|
7071
|
+
leveragedEtfsEnabled: booleanType().default(false),
|
|
7072
|
+
inverseEtfsEnabled: booleanType().default(false),
|
|
7073
|
+
memeStocksEnabled: booleanType().default(false),
|
|
7074
|
+
ipoParticipationEnabled: booleanType().default(false),
|
|
7075
|
+
borrowAvailabilityRequired: booleanType().default(true),
|
|
7076
|
+
maxBorrowFeePct: numberType().min(0).default(5),
|
|
7077
|
+
});
|
|
7078
|
+
const AssetUniversePrefsSchema = AssetUniversePrefsObjectSchema.default({});
|
|
7079
|
+
|
|
7080
|
+
/**
|
|
7081
|
+
* Risk budget preferences schema (section 7.3).
|
|
7082
|
+
* Controls portfolio-level risk limits including concentration caps,
|
|
7083
|
+
* drawdown thresholds, loss limits, and exposure constraints.
|
|
7084
|
+
*
|
|
7085
|
+
* The raw ZodObject variant (`RiskBudgetPrefsObjectSchema`) is exported
|
|
7086
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7087
|
+
*/
|
|
7088
|
+
const RiskBudgetPrefsObjectSchema = objectType({
|
|
7089
|
+
maxMarginUtilPct: numberType().min(0).max(100).default(50),
|
|
7090
|
+
maxIssuerConcentrationPct: numberType().min(0).max(100).default(20),
|
|
7091
|
+
maxThemeConcentrationPct: numberType().min(0).max(100).default(25),
|
|
7092
|
+
maxAssetClassConcentrationPct: numberType().min(0).max(100).default(50),
|
|
7093
|
+
maxCountryConcentrationPct: numberType().min(0).max(100).default(40),
|
|
7094
|
+
maxCurrencyExposurePct: numberType().min(0).max(100).default(30),
|
|
7095
|
+
maxCorrelatedExposurePct: numberType().min(0).max(100).default(40),
|
|
7096
|
+
betaTarget: numberType().nullable().default(null),
|
|
7097
|
+
maxBeta: numberType().min(0).default(2),
|
|
7098
|
+
// Tighter per-trade risk (1.5% vs 2%) for scalping — high turnover means
|
|
7099
|
+
// many small risks compound; per-trade ceiling must be lower than the
|
|
7100
|
+
// swing default to keep daily VAR bounded.
|
|
7101
|
+
maxRiskPerTradePct: numberType().min(0).max(100).default(1.5),
|
|
7102
|
+
// 2% daily loss circuit breaker (vs 5%) — tighter to fail fast when the
|
|
7103
|
+
// regime turns against the scalping strategy.
|
|
7104
|
+
maxLossPerDayPct: numberType().min(0).max(100).default(2.0),
|
|
7105
|
+
// 5% weekly loss cap (vs 10%) — proportional to the daily reduction.
|
|
7106
|
+
maxLossPerWeekPct: numberType().min(0).max(100).default(5.0),
|
|
7107
|
+
maxLossPerMonthPct: numberType().min(0).max(100).default(15),
|
|
7108
|
+
maxDrawdownFromPeakPct: numberType().min(0).max(100).default(20),
|
|
7109
|
+
maxSimultaneousSignalsPerStrategy: numberType().min(0).default(5),
|
|
7110
|
+
maxAggregateHeatPct: numberType().min(0).max(100).default(80),
|
|
7111
|
+
overnightExposureCapPct: numberType().min(0).max(100).default(50),
|
|
7112
|
+
weekendExposureCapPct: numberType().min(0).max(100).default(30),
|
|
7113
|
+
eventRiskExposureCapPct: numberType().min(0).max(100).default(40),
|
|
7114
|
+
gapRiskSensitivity: enumType(["low", "medium", "high"]).default("medium"),
|
|
7115
|
+
/**
|
|
7116
|
+
* Per-trade equity allocation as % of account equity. Replaces legacy AlpacaAccount.tradeAllocationPct.
|
|
7117
|
+
* Smaller per-trade size (2% vs 5%) for scalping — shorter holds + higher
|
|
7118
|
+
* concurrency demand smaller per-position bets.
|
|
7119
|
+
*/
|
|
7120
|
+
perTradeAllocationPct: numberType().min(0).max(100).default(2),
|
|
7121
|
+
/** Per-trade crypto allocation as % of account equity. Replaces legacy AlpacaAccount.cryptoTradeAllocationPct. */
|
|
7122
|
+
perTradeCryptoAllocationPct: numberType().min(0).max(100).default(5),
|
|
7123
|
+
/** Alpaca day-trading buying power check enforcement. Synced to Alpaca API. */
|
|
7124
|
+
dtbpCheck: enumType(["both", "entry", "exit"]).default("both"),
|
|
7125
|
+
/** Alpaca pattern day trader rule enforcement. Synced to Alpaca API. */
|
|
7126
|
+
pdtCheck: enumType(["both", "entry", "exit"]).default("both"),
|
|
7127
|
+
/** Strict PDT enforcement — block all violations without exception. Synced to Alpaca API. */
|
|
7128
|
+
ptpNoExceptionEntry: booleanType().default(false),
|
|
7129
|
+
});
|
|
7130
|
+
const RiskBudgetPrefsSchema = RiskBudgetPrefsObjectSchema.default({});
|
|
7131
|
+
|
|
7132
|
+
/** Schema for a named no-trade window (e.g., lunch hour, FOMC). */
|
|
7133
|
+
const NoTradeWindowSchema = objectType({
|
|
7134
|
+
name: stringType(),
|
|
7135
|
+
startTime: stringType(),
|
|
7136
|
+
endTime: stringType(),
|
|
7137
|
+
daysOfWeek: arrayType(numberType().min(0).max(6)).default([]),
|
|
7138
|
+
enabled: booleanType().default(true),
|
|
7139
|
+
});
|
|
7140
|
+
/** Schema for per-strategy priority assignment. */
|
|
7141
|
+
const StrategyPriorityRuleSchema = objectType({
|
|
7142
|
+
strategy: stringType(),
|
|
7143
|
+
priority: numberType(),
|
|
7144
|
+
});
|
|
7145
|
+
/**
|
|
7146
|
+
* Signal consumption preferences schema (section 7.4).
|
|
7147
|
+
* Controls how incoming trade signals are filtered, throttled,
|
|
7148
|
+
* prioritized, and handled when conflicts arise.
|
|
7149
|
+
*
|
|
7150
|
+
* The raw ZodObject variant (`SignalConsumptionPrefsObjectSchema`) is exported
|
|
7151
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7152
|
+
*/
|
|
7153
|
+
const SignalConsumptionPrefsObjectSchema = objectType({
|
|
7154
|
+
enabledStrategies: arrayType(stringType()).default([]),
|
|
7155
|
+
disabledStrategies: arrayType(stringType()).default([]),
|
|
7156
|
+
// Slightly higher confidence floor (65 vs 60) reflecting the precision
|
|
7157
|
+
// demands of scalping — false positives compound rapidly at this cadence.
|
|
7158
|
+
minConfidenceByDefault: numberType().min(0).max(100).default(65),
|
|
7159
|
+
minConfidenceByAssetClass: recordType(stringType(), numberType()).default({}),
|
|
7160
|
+
minConfidenceByStrategy: recordType(stringType(), numberType()).default({}),
|
|
7161
|
+
// 1.3 R:R minimum — slightly tighter than the swing default (1.5) since
|
|
7162
|
+
// scalp trades have quicker resolution and tolerate marginally lower
|
|
7163
|
+
// expected R:R when win-rate is high.
|
|
7164
|
+
minExpectedRewardRiskRatio: numberType().min(0).default(1.3),
|
|
7165
|
+
minExpectedEdgePct: numberType().min(0).default(0),
|
|
7166
|
+
// 30s max signal age for scalping — anything older than that has likely
|
|
7167
|
+
// been re-priced out of edge. Previous default (300s) was swing-suitable.
|
|
7168
|
+
maxSignalAgeSeconds: numberType().min(0).default(30),
|
|
7169
|
+
// 5s post-entry cooldown — minimum churn protection while still allowing
|
|
7170
|
+
// rapid re-engagement if the setup persists.
|
|
7171
|
+
cooldownAfterEntrySeconds: numberType().min(0).default(5),
|
|
7172
|
+
cooldownAfterExitSeconds: numberType().min(0).default(120),
|
|
7173
|
+
// 30s post-stop-out cooldown (vs 300s) — fast re-entry allowed once the
|
|
7174
|
+
// setup re-presents.
|
|
7175
|
+
cooldownAfterStopOutSeconds: numberType().min(0).default(30),
|
|
7176
|
+
cooldownAfterFailedTradeSeconds: numberType().min(0).default(180),
|
|
7177
|
+
// 10s duplicate-signal window — prevents same-second signal dedup but
|
|
7178
|
+
// allows the same setup to trigger again within a minute.
|
|
7179
|
+
duplicateSignalSuppressionWindowSeconds: numberType().min(0).default(10),
|
|
7180
|
+
reversalHandlingPolicy: enumType([
|
|
7181
|
+
"ignore_reversal",
|
|
7182
|
+
"close_only",
|
|
7183
|
+
"flatten_then_reverse",
|
|
7184
|
+
"allow_full_reversal",
|
|
7185
|
+
])
|
|
7186
|
+
.default("close_only"),
|
|
7187
|
+
conflictHandlingOpenOrders: enumType([
|
|
7188
|
+
"cancel_conflicting",
|
|
7189
|
+
"replace_existing",
|
|
7190
|
+
"keep_existing_skip",
|
|
7191
|
+
"escalate",
|
|
7192
|
+
])
|
|
7193
|
+
.default("cancel_conflicting"),
|
|
7194
|
+
conflictHandlingOpposingPosition: enumType(["reduce", "close", "flatten_then_reverse", "hold"])
|
|
7195
|
+
.default("close"),
|
|
7196
|
+
minConvictionDeltaToModify: numberType().min(0).max(100).default(10),
|
|
7197
|
+
strategyPriorityRules: arrayType(StrategyPriorityRuleSchema).default([]),
|
|
7198
|
+
noTradeWindows: arrayType(NoTradeWindowSchema).default([]),
|
|
7199
|
+
earningsBlackoutEnabled: booleanType().default(false),
|
|
7200
|
+
earningsBlackoutHoursBefore: numberType().min(0).default(24),
|
|
7201
|
+
earningsBlackoutHoursAfter: numberType().min(0).default(2),
|
|
7202
|
+
/**
|
|
7203
|
+
* Minimum price movement % to qualify as a tradeable signal. Replaces legacy AlpacaAccount.minPercentageChange.
|
|
7204
|
+
* Tighter intraday move filter (15bps vs 50bps) — scalping captures
|
|
7205
|
+
* sub-percent moves that the swing-trading default would have ignored.
|
|
7206
|
+
*/
|
|
7207
|
+
minPercentageChange: numberType().min(0).default(0.15),
|
|
7208
|
+
/**
|
|
7209
|
+
* Minimum average daily volume to qualify a symbol for trading. Replaces legacy AlpacaAccount.volumeThreshold.
|
|
7210
|
+
* Higher floor (100k vs 50k) — scalping requires consistent liquidity to
|
|
7211
|
+
* keep slippage within the tightened maxSlippageTolerancePct (0.3%).
|
|
7212
|
+
*/
|
|
7213
|
+
volumeThreshold: numberType().min(0).default(100000),
|
|
7214
|
+
});
|
|
7215
|
+
const SignalConsumptionPrefsSchema = SignalConsumptionPrefsObjectSchema.default({});
|
|
7216
|
+
|
|
7217
|
+
const OrderTypeEnum = enumType([
|
|
7218
|
+
"market",
|
|
7219
|
+
"limit",
|
|
7220
|
+
"stop",
|
|
7221
|
+
"stop_limit",
|
|
7222
|
+
"trailing_stop",
|
|
7223
|
+
]);
|
|
7224
|
+
/**
|
|
7225
|
+
* Execution preferences schema (section 7.5).
|
|
7226
|
+
* Governs order routing behavior including order types, time-in-force,
|
|
7227
|
+
* slippage tolerance, price collars, partial fill handling, and failure modes.
|
|
7228
|
+
*
|
|
7229
|
+
* The raw ZodObject variant (`ExecutionPrefsObjectSchema`) is exported
|
|
7230
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7231
|
+
*/
|
|
7232
|
+
const ExecutionPrefsObjectSchema = objectType({
|
|
7233
|
+
allowedOrderTypes: arrayType(OrderTypeEnum)
|
|
7234
|
+
.default(["market", "limit", "stop", "trailing_stop"]),
|
|
7235
|
+
preferredOrderType: OrderTypeEnum.default("limit"),
|
|
7236
|
+
preferredOrderTypeByAssetClass: recordType(stringType(), stringType())
|
|
7237
|
+
.default({ crypto: "market" }),
|
|
7238
|
+
// IOC default (immediate-or-cancel) for scalping — orders that don't
|
|
7239
|
+
// fill instantly should be killed, not parked on the book where they
|
|
7240
|
+
// accumulate stale-price risk.
|
|
7241
|
+
defaultTimeInForce: enumType(["day", "gtc", "ioc", "fok"]).default("ioc"),
|
|
7242
|
+
// Passive bias — scalping captures spread by posting on the book rather
|
|
7243
|
+
// than paying it. Aggressive crossings are reserved for risk-off / unwind.
|
|
7244
|
+
executionBias: enumType(["passive", "neutral", "aggressive"])
|
|
7245
|
+
.default("passive"),
|
|
7246
|
+
// 30bps slippage tolerance (vs 100bps) — must be tighter than scalping
|
|
7247
|
+
// edge magnitudes (typically ~50-100bps) to preserve PnL.
|
|
7248
|
+
maxSlippageTolerancePct: numberType().min(0).max(100).default(0.3),
|
|
7249
|
+
priceCollarEnabled: booleanType().default(true),
|
|
7250
|
+
// 50bps price collar (vs 200bps) — same logic as slippage tolerance:
|
|
7251
|
+
// collar must sit inside the edge magnitude.
|
|
7252
|
+
priceCollarPct: numberType().min(0).default(0.5),
|
|
7253
|
+
repriceEnabled: booleanType().default(false),
|
|
7254
|
+
repriceMaxAttempts: numberType().min(0).default(3),
|
|
7255
|
+
repriceIntervalSeconds: numberType().min(0).default(30),
|
|
7256
|
+
cancelReplaceTimeoutSeconds: numberType().min(0).default(60),
|
|
7257
|
+
partialFillPolicy: enumType(["accept_partial", "cancel_remainder", "replace_to_fill"])
|
|
7258
|
+
.default("accept_partial"),
|
|
7259
|
+
sizingMethod: enumType(["notional", "quantity"]).default("notional"),
|
|
7260
|
+
lotRoundingBehavior: enumType(["round_down", "round_nearest", "round_up"])
|
|
7261
|
+
.default("round_down"),
|
|
7262
|
+
afterHoursExecutionBehavior: enumType(["limit_only", "no_execution", "normal"])
|
|
7263
|
+
.default("limit_only"),
|
|
7264
|
+
failureBehavior: enumType(["fail_safe", "fail_open"]).default("fail_safe"),
|
|
7265
|
+
});
|
|
7266
|
+
const ExecutionPrefsSchema = ExecutionPrefsObjectSchema.default({});
|
|
7267
|
+
|
|
7268
|
+
/** Schema for trailing stop tightening rules that adjust stop distance at profit thresholds. */
|
|
7269
|
+
const TrailingStopTighteningRuleSchema = objectType({
|
|
7270
|
+
profitThresholdPct: numberType().min(0),
|
|
7271
|
+
newTrailPct: numberType().min(0),
|
|
7272
|
+
});
|
|
7273
|
+
/**
|
|
7274
|
+
* Position management preferences schema (section 7.6).
|
|
7275
|
+
* Governs stop-loss methods, take-profit targets, scaling behavior,
|
|
7276
|
+
* holding period limits, and automatic position closure rules.
|
|
7277
|
+
*
|
|
7278
|
+
* The raw ZodObject variant (`PositionManagementPrefsObjectSchema`) is exported
|
|
7279
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7280
|
+
*/
|
|
7281
|
+
const PositionManagementPrefsObjectSchema = objectType({
|
|
7282
|
+
defaultStopLossMethod: enumType(["fixed_percent", "atr_based", "structure_based", "trailing_stop"])
|
|
7283
|
+
.default("trailing_stop"),
|
|
7284
|
+
// 50bps stop sized for 5-min bar scalp profiles (audit 2026-05-10).
|
|
7285
|
+
defaultStopLossPct: numberType().min(0).max(100).default(0.5),
|
|
7286
|
+
// 0.75x ATR multiplier — tighter than the previous 2x to keep ATR-based
|
|
7287
|
+
// stops aligned with sub-percent intraday move ranges.
|
|
7288
|
+
atrStopMultiplier: numberType().min(0).default(0.75),
|
|
7289
|
+
defaultTakeProfitMethod: enumType(["fixed_percent", "atr_based", "risk_reward_ratio", "none"])
|
|
7290
|
+
.default("risk_reward_ratio"),
|
|
7291
|
+
// 100bps take-profit pairs with the 50bps stop at a 2:1 R:R via the
|
|
7292
|
+
// explicit defaultRiskRewardRatio below; both serve different code paths
|
|
7293
|
+
// (fixed-percent vs ratio-derived TP).
|
|
7294
|
+
defaultTakeProfitPct: numberType().min(0).max(100).default(1.0),
|
|
7295
|
+
defaultRiskRewardRatio: numberType().min(0).default(1.5),
|
|
7296
|
+
breakEvenStopEnabled: booleanType().default(true),
|
|
7297
|
+
// Move stop to break-even after 1% of profit (vs 2%) — tighter to match
|
|
7298
|
+
// scalping cadence.
|
|
7299
|
+
breakEvenTriggerPct: numberType().min(0).max(100).default(1),
|
|
7300
|
+
scaleInEnabled: booleanType().default(false),
|
|
7301
|
+
scaleInMaxAdds: numberType().min(0).default(2),
|
|
7302
|
+
scaleOutEnabled: booleanType().default(true),
|
|
7303
|
+
scaleOutTrimPct: numberType().min(0).max(100).default(50),
|
|
7304
|
+
// Trim 50% at +0.5% — much earlier than the previous 5% for scalping.
|
|
7305
|
+
scaleOutTriggerPct: numberType().min(0).max(100).default(0.5),
|
|
7306
|
+
// Hard 30-min intraday hold cap. Previous default of 0 (unlimited) is
|
|
7307
|
+
// wrong for scalping where stale positions accumulate undefined risk.
|
|
7308
|
+
maxHoldingPeriodMinutes: numberType().min(0).default(30),
|
|
7309
|
+
maxHoldingPeriodByAssetClass: recordType(stringType(), numberType()).default({}),
|
|
7310
|
+
dayTradeOnly: booleanType().default(false),
|
|
7311
|
+
autoCloseBeforeEarnings: booleanType().default(false),
|
|
7312
|
+
autoCloseBeforeClose: booleanType().default(false),
|
|
7313
|
+
autoCloseBeforeCloseMinutes: numberType().min(0).default(5),
|
|
7314
|
+
autoCloseBeforeWeekend: booleanType().default(false),
|
|
7315
|
+
addToWinnersAllowed: booleanType().default(true),
|
|
7316
|
+
addToLosersAllowed: booleanType().default(false),
|
|
7317
|
+
stopWideningAllowed: booleanType().default(false),
|
|
7318
|
+
trailingStopTighteningEnabled: booleanType().default(true),
|
|
7319
|
+
trailingStopTighteningRules: arrayType(TrailingStopTighteningRuleSchema)
|
|
7320
|
+
.default([
|
|
7321
|
+
{ profitThresholdPct: 3, newTrailPct: 2.0 },
|
|
7322
|
+
{ profitThresholdPct: 6, newTrailPct: 1.5 },
|
|
7323
|
+
{ profitThresholdPct: 10, newTrailPct: 1.0 },
|
|
7324
|
+
]),
|
|
7325
|
+
portfolioStopOverridesPositionStops: booleanType().default(false),
|
|
7326
|
+
// 10-min stop-out cooldown (vs 30) — fast re-entry permitted once the
|
|
7327
|
+
// adverse regime resolves.
|
|
7328
|
+
doNotReenterAfterStopOutMinutes: numberType().min(0).default(10),
|
|
7329
|
+
doNotReenterAfterForcedCloseMinutes: numberType().min(0).default(60),
|
|
7330
|
+
});
|
|
7331
|
+
const PositionManagementPrefsSchema = PositionManagementPrefsObjectSchema.default({});
|
|
7332
|
+
|
|
7333
|
+
/** Schema for tactical allocation band constraints around a target weight. */
|
|
7334
|
+
const TacticalBandSchema = objectType({
|
|
7335
|
+
target: numberType(),
|
|
7336
|
+
minPct: numberType(),
|
|
7337
|
+
maxPct: numberType(),
|
|
7338
|
+
});
|
|
7339
|
+
/** Schema for strategy sleeve budget allocation tracking. */
|
|
7340
|
+
const StrategySleeveBudgetSchema = objectType({
|
|
7341
|
+
maxAllocationPct: numberType(),
|
|
7342
|
+
currentAllocationPct: numberType().default(0),
|
|
7343
|
+
});
|
|
7344
|
+
/** Schema for defensive cash escalation trigger conditions. */
|
|
7345
|
+
const DefensiveCashRuleSchema = objectType({
|
|
7346
|
+
triggerCondition: stringType(),
|
|
7347
|
+
targetCashPct: numberType(),
|
|
7348
|
+
});
|
|
7349
|
+
/**
|
|
7350
|
+
* Portfolio construction preferences schema (section 7.7).
|
|
7351
|
+
* Governs asset allocation targets, rebalancing triggers, weighting methods,
|
|
7352
|
+
* cash management, portfolio-level stops, and forced deleveraging rules.
|
|
7353
|
+
*
|
|
7354
|
+
* The raw ZodObject variant (`PortfolioConstructionPrefsObjectSchema`) is exported
|
|
7355
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7356
|
+
*/
|
|
7357
|
+
const PortfolioConstructionPrefsObjectSchema = objectType({
|
|
7358
|
+
targetAllocationByAssetClass: recordType(stringType(), numberType()).default({}),
|
|
7359
|
+
tacticalAllocationBands: recordType(stringType(), TacticalBandSchema).default({}),
|
|
7360
|
+
driftThresholdPct: numberType().min(0).max(100).default(5),
|
|
7361
|
+
rebalanceTrigger: enumType(["threshold", "calendar", "both"])
|
|
7362
|
+
.default("threshold"),
|
|
7363
|
+
rebalanceFrequencyDays: numberType().min(0).default(30),
|
|
7364
|
+
autonomousRebalancing: booleanType().default(false),
|
|
7365
|
+
rebalanceDuringRiskOff: booleanType().default(false),
|
|
7366
|
+
maxTurnoverPerRebalancePct: numberType().min(0).max(100).default(20),
|
|
7367
|
+
preferredWeighting: enumType([
|
|
7368
|
+
"equal_weight",
|
|
7369
|
+
"risk_based",
|
|
7370
|
+
"conviction_weighted",
|
|
7371
|
+
"target_allocation",
|
|
7372
|
+
])
|
|
7373
|
+
.default("equal_weight"),
|
|
7374
|
+
strategySleeveBudgets: recordType(stringType(), StrategySleeveBudgetSchema)
|
|
7375
|
+
.default({}),
|
|
7376
|
+
cashTargetPct: numberType().min(0).max(100).default(10),
|
|
7377
|
+
defensiveCashEscalationEnabled: booleanType().default(false),
|
|
7378
|
+
defensiveCashEscalationRules: arrayType(DefensiveCashRuleSchema).default([]),
|
|
7379
|
+
hedgeOverlayEnabled: booleanType().default(false),
|
|
7380
|
+
autonomousPortfolioTrailingStop: booleanType().default(false),
|
|
7381
|
+
portfolioTrailingStopPct: numberType().min(0).max(100).default(5),
|
|
7382
|
+
portfolioCircuitBreakerEnabled: booleanType().default(true),
|
|
7383
|
+
portfolioCircuitBreakerDrawdownPct: numberType().min(0).max(100).default(10),
|
|
7384
|
+
equityCurveStopEnabled: booleanType().default(false),
|
|
7385
|
+
equityCurveStopLookbackDays: numberType().min(0).default(20),
|
|
7386
|
+
forcedDeleveragingEnabled: booleanType().default(true),
|
|
7387
|
+
forcedDeleveragingTriggerDrawdownPct: numberType().min(0).max(100).default(15),
|
|
7388
|
+
forcedDeleveragingTargetExposurePct: numberType().min(0).max(100).default(50),
|
|
7389
|
+
});
|
|
7390
|
+
const PortfolioConstructionPrefsSchema = PortfolioConstructionPrefsObjectSchema.default({});
|
|
7391
|
+
|
|
7392
|
+
/** Schema for the response configuration applied when a specific overlay type activates. */
|
|
7393
|
+
const OverlayResponseConfigSchema = objectType({
|
|
7394
|
+
pauseRealtimeTrading: booleanType().default(false),
|
|
7395
|
+
pauseNewEntriesOnly: booleanType().default(false),
|
|
7396
|
+
cancelAllOpenOrders: booleanType().default(false),
|
|
7397
|
+
tightenStops: booleanType().default(false),
|
|
7398
|
+
tightenStopsByPct: numberType().min(0).max(100).default(50),
|
|
7399
|
+
reduceRiskBudgets: booleanType().default(false),
|
|
7400
|
+
reduceRiskBudgetsByPct: numberType().min(0).max(100).default(50),
|
|
7401
|
+
rebalanceToTargetAllocation: booleanType().default(false),
|
|
7402
|
+
raiseCash: booleanType().default(false),
|
|
7403
|
+
raiseCashTargetPct: numberType().min(0).max(100).default(30),
|
|
7404
|
+
unwindSpecificSectors: arrayType(stringType()).default([]),
|
|
7405
|
+
unwindSpecificAssetClasses: arrayType(stringType()).default([]),
|
|
7406
|
+
closeAllPositions: booleanType().default(false),
|
|
7407
|
+
switchToManualApproval: booleanType().default(false),
|
|
7408
|
+
downgradeExecutionAggressiveness: booleanType().default(false),
|
|
7409
|
+
switchModelTier: stringType().nullable().default(null),
|
|
7410
|
+
extendCooldowns: booleanType().default(false),
|
|
7411
|
+
extendCooldownMultiplier: numberType().min(1).default(2),
|
|
7412
|
+
});
|
|
7413
|
+
/**
|
|
7414
|
+
* Overlay response preferences schema (section 7.8).
|
|
7415
|
+
* Maps overlay types to specific response configurations that modify
|
|
7416
|
+
* trading behavior when protective overlays are triggered.
|
|
7417
|
+
*
|
|
7418
|
+
* The raw ZodObject variant (`OverlayResponsePrefsObjectSchema`) is exported
|
|
7419
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7420
|
+
*/
|
|
7421
|
+
const OverlayResponsePrefsObjectSchema = objectType({
|
|
7422
|
+
overlayResponses: recordType(nativeEnumType(OverlayType), OverlayResponseConfigSchema)
|
|
7423
|
+
.default({}),
|
|
7424
|
+
});
|
|
7425
|
+
const OverlayResponsePrefsSchema = OverlayResponsePrefsObjectSchema.default({});
|
|
7426
|
+
|
|
7427
|
+
/** Schema for a single entry in an LLM fallback chain. */
|
|
7428
|
+
const FallbackChainEntrySchema = objectType({
|
|
7429
|
+
provider: nativeEnumType(LlmProvider),
|
|
7430
|
+
modelId: stringType(),
|
|
7431
|
+
});
|
|
7432
|
+
/** Schema for tool use permissions granted to a specific model tier. */
|
|
7433
|
+
const ToolUsePermissionsSchema = objectType({
|
|
7434
|
+
readTools: booleanType().default(true),
|
|
7435
|
+
writeTools: booleanType().default(false),
|
|
7436
|
+
});
|
|
7437
|
+
/**
|
|
7438
|
+
* Model preferences schema (section 7.9).
|
|
7439
|
+
* Governs LLM provider fallback chains, escalation triggers, cost limits,
|
|
7440
|
+
* latency targets, tool permissions by tier, and memory retention settings.
|
|
7441
|
+
*
|
|
7442
|
+
* The raw ZodObject variant (`ModelPrefsObjectSchema`) is exported
|
|
7443
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7444
|
+
*/
|
|
7445
|
+
const ModelPrefsObjectSchema = objectType({
|
|
7446
|
+
miniFallbackChain: arrayType(FallbackChainEntrySchema).default([]),
|
|
7447
|
+
normalFallbackChain: arrayType(FallbackChainEntrySchema).default([]),
|
|
7448
|
+
advancedFallbackChain: arrayType(FallbackChainEntrySchema).default([]),
|
|
7449
|
+
escalateToAdvancedWhenConflicts: booleanType().default(true),
|
|
7450
|
+
escalateToAdvancedWhenHighExposure: booleanType().default(true),
|
|
7451
|
+
highExposureThresholdPct: numberType().min(0).max(100).default(50),
|
|
7452
|
+
escalateToAdvancedWhenMultipleOrders: booleanType().default(true),
|
|
7453
|
+
multipleOrdersThreshold: numberType().min(0).default(3),
|
|
7454
|
+
maxCostPerDayUsd: numberType().min(0).default(10),
|
|
7455
|
+
maxCostPerDecisionUsd: numberType().min(0).default(1),
|
|
7456
|
+
latencyTargetMs: numberType().min(0).default(5000),
|
|
7457
|
+
timeoutMs: numberType().min(0).default(30000),
|
|
7458
|
+
maxRetries: numberType().min(0).default(2),
|
|
7459
|
+
toolUsePermissionsByTier: recordType(stringType(), ToolUsePermissionsSchema)
|
|
7460
|
+
.default({
|
|
7461
|
+
mini: { readTools: true, writeTools: false },
|
|
7462
|
+
normal: { readTools: true, writeTools: false },
|
|
7463
|
+
advanced: { readTools: true, writeTools: true },
|
|
7464
|
+
}),
|
|
7465
|
+
memorySummaryCadenceMinutes: numberType().min(0).default(60),
|
|
7466
|
+
maxMemorySummariesRetained: numberType().min(0).default(50),
|
|
7467
|
+
excludedProvidersForWorkflows: recordType(stringType(), arrayType(stringType()))
|
|
7468
|
+
.default({}),
|
|
7469
|
+
quantModelWeight: numberType().min(0).max(1).default(0.7),
|
|
7470
|
+
});
|
|
7471
|
+
const ModelPrefsSchema = ModelPrefsObjectSchema.default({});
|
|
7472
|
+
|
|
7473
|
+
/**
|
|
7474
|
+
* Audit and notification preferences schema (section 7.10).
|
|
7475
|
+
* Controls notification triggers, audit detail levels, artifact retention,
|
|
7476
|
+
* incident alert channels, and decision trace persistence.
|
|
7477
|
+
*
|
|
7478
|
+
* The raw ZodObject variant (`AuditNotificationPrefsObjectSchema`) is exported
|
|
7479
|
+
* for use with `deepPartial()`, which requires a ZodObject (not ZodDefault).
|
|
7480
|
+
*/
|
|
7481
|
+
const AuditNotificationPrefsObjectSchema = objectType({
|
|
7482
|
+
notifyOnAutonomousActions: booleanType().default(true),
|
|
7483
|
+
notifyOnBlockedTrades: booleanType().default(true),
|
|
7484
|
+
notifyOnOverlayActivation: booleanType().default(true),
|
|
7485
|
+
notifyOnPolicyMutation: booleanType().default(true),
|
|
7486
|
+
dailySummaryEnabled: booleanType().default(true),
|
|
7487
|
+
eventSummaryEnabled: booleanType().default(true),
|
|
7488
|
+
auditDetailLevel: enumType(["minimal", "standard", "verbose"])
|
|
7489
|
+
.default("standard"),
|
|
7490
|
+
saveRationaleSummaries: booleanType().default(true),
|
|
7491
|
+
saveToolCallTraces: booleanType().default(false),
|
|
7492
|
+
saveContextSnapshots: booleanType().default(true),
|
|
7493
|
+
incidentAlertChannel: enumType(["email", "slack", "discord", "webhook", "none"])
|
|
7494
|
+
.default("none"),
|
|
7495
|
+
incidentAlertEndpoint: stringType().default(""),
|
|
7496
|
+
requirePostActionExplanation: booleanType().default(true),
|
|
7497
|
+
retainDecisionArtifactsDays: numberType().min(0).default(90),
|
|
7498
|
+
/** Trade confirmation email preference. Synced to Alpaca API. */
|
|
7499
|
+
tradeConfirmEmail: enumType(["all", "none"]).default("all"),
|
|
7500
|
+
});
|
|
7501
|
+
const AuditNotificationPrefsSchema = AuditNotificationPrefsObjectSchema.default({});
|
|
7502
|
+
|
|
7503
|
+
/**
|
|
7504
|
+
* Policy mutation schema for partial updates to a trading policy.
|
|
7505
|
+
* All fields are optional to allow selective mutation of individual settings.
|
|
7506
|
+
* Uses `deepPartial()` on nested preference schemas so callers can update
|
|
7507
|
+
* a single leaf field without supplying the entire sub-object.
|
|
7508
|
+
* Uses `passthrough()` to allow additional fields for forward-compatibility.
|
|
7509
|
+
*/
|
|
7510
|
+
objectType({
|
|
7511
|
+
autonomyMode: nativeEnumType(AutonomyMode).optional(),
|
|
7512
|
+
realtimeTradingEnabled: booleanType().optional(),
|
|
7513
|
+
paperTradingOnly: booleanType().optional(),
|
|
7514
|
+
killSwitchEnabled: booleanType().optional(),
|
|
7515
|
+
equitiesEnabled: booleanType().optional(),
|
|
7516
|
+
etfsEnabled: booleanType().optional(),
|
|
7517
|
+
cryptoEnabled: booleanType().optional(),
|
|
7518
|
+
optionsEnabled: booleanType().optional(),
|
|
7519
|
+
futuresEnabled: booleanType().optional(),
|
|
7520
|
+
forexEnabled: booleanType().optional(),
|
|
7521
|
+
shortingEnabled: booleanType().optional(),
|
|
7522
|
+
marginEnabled: booleanType().optional(),
|
|
7523
|
+
fractionalSharesEnabled: booleanType().optional(),
|
|
7524
|
+
maxBuyingPowerUtilPct: numberType().optional(),
|
|
7525
|
+
cashFloorPct: numberType().optional(),
|
|
7526
|
+
maxGrossExposurePct: numberType().optional(),
|
|
7527
|
+
maxNetExposurePct: numberType().optional(),
|
|
7528
|
+
maxLeverage: numberType().optional(),
|
|
7529
|
+
maxSymbolConcentrationPct: numberType().optional(),
|
|
7530
|
+
maxSectorConcentrationPct: numberType().optional(),
|
|
7531
|
+
maxOpenPositions: numberType().optional(),
|
|
7532
|
+
maxOpenOrders: numberType().optional(),
|
|
7533
|
+
miniModelProvider: nativeEnumType(LlmProvider).nullable().optional(),
|
|
7534
|
+
miniModelId: stringType().nullable().optional(),
|
|
7535
|
+
normalModelProvider: nativeEnumType(LlmProvider).nullable().optional(),
|
|
7536
|
+
normalModelId: stringType().nullable().optional(),
|
|
7537
|
+
advancedModelProvider: nativeEnumType(LlmProvider).nullable().optional(),
|
|
7538
|
+
advancedModelId: stringType().nullable().optional(),
|
|
7539
|
+
autonomyPrefs: AutonomyPrefsObjectSchema.deepPartial().optional(),
|
|
7540
|
+
assetUniversePrefs: AssetUniversePrefsObjectSchema.deepPartial().optional(),
|
|
7541
|
+
riskBudgetPrefs: RiskBudgetPrefsObjectSchema.deepPartial().optional(),
|
|
7542
|
+
signalConsumptionPrefs: SignalConsumptionPrefsObjectSchema.deepPartial().optional(),
|
|
7543
|
+
executionPrefs: ExecutionPrefsObjectSchema.deepPartial().optional(),
|
|
7544
|
+
positionManagementPrefs: PositionManagementPrefsObjectSchema.deepPartial().optional(),
|
|
7545
|
+
portfolioConstructionPrefs: PortfolioConstructionPrefsObjectSchema.deepPartial().optional(),
|
|
7546
|
+
overlayResponsePrefs: OverlayResponsePrefsObjectSchema.deepPartial().optional(),
|
|
7547
|
+
modelPrefs: ModelPrefsObjectSchema.deepPartial().optional(),
|
|
7548
|
+
auditNotificationPrefs: AuditNotificationPrefsObjectSchema.deepPartial().optional(),
|
|
7549
|
+
})
|
|
7550
|
+
.passthrough();
|
|
7551
|
+
|
|
7552
|
+
/**
|
|
7553
|
+
* Effective trading policy schema representing the fully-resolved policy
|
|
7554
|
+
* with all required fields populated. This is the runtime contract for the
|
|
7555
|
+
* trading engine -- every field is required (no optionals).
|
|
7556
|
+
*/
|
|
7557
|
+
const EffectiveTradingPolicySchema = objectType({
|
|
7558
|
+
autonomyMode: nativeEnumType(AutonomyMode),
|
|
7559
|
+
realtimeTradingEnabled: booleanType(),
|
|
7560
|
+
paperTradingOnly: booleanType(),
|
|
7561
|
+
killSwitchEnabled: booleanType(),
|
|
7562
|
+
equitiesEnabled: booleanType(),
|
|
7563
|
+
etfsEnabled: booleanType(),
|
|
7564
|
+
cryptoEnabled: booleanType(),
|
|
7565
|
+
optionsEnabled: booleanType(),
|
|
7566
|
+
futuresEnabled: booleanType(),
|
|
7567
|
+
forexEnabled: booleanType(),
|
|
7568
|
+
shortingEnabled: booleanType(),
|
|
7569
|
+
marginEnabled: booleanType(),
|
|
7570
|
+
fractionalSharesEnabled: booleanType(),
|
|
7571
|
+
maxBuyingPowerUtilPct: numberType(),
|
|
7572
|
+
cashFloorPct: numberType(),
|
|
7573
|
+
maxGrossExposurePct: numberType(),
|
|
7574
|
+
maxNetExposurePct: numberType(),
|
|
7575
|
+
maxLeverage: numberType(),
|
|
7576
|
+
maxSymbolConcentrationPct: numberType(),
|
|
7577
|
+
maxSectorConcentrationPct: numberType(),
|
|
7578
|
+
maxOpenPositions: numberType(),
|
|
7579
|
+
maxOpenOrders: numberType(),
|
|
7580
|
+
/** Percentage of account equity (with leverage) allocated per equities trade. Canonical replacement for legacy AlpacaAccount.tradeAllocationPct. */
|
|
7581
|
+
perTradeEquityAllocationPct: numberType(),
|
|
7582
|
+
/** Percentage of the crypto allocation slice of account equity allocated per crypto trade. Canonical replacement for legacy AlpacaAccount.cryptoTradeAllocationPct. */
|
|
7583
|
+
perTradeCryptoAllocationPct: numberType(),
|
|
7584
|
+
/**
|
|
7585
|
+
* Wash-trade cooldown period in milliseconds for equities. Defines the
|
|
7586
|
+
* minimum elapsed time between opposing-side fills on the same symbol per
|
|
7587
|
+
* FINRA Rule 5210, mirroring the 30s default already enforced for crypto.
|
|
7588
|
+
* Mirrors `TradingPolicy.equityWashTradeCooldownMs` in backend-legacy.
|
|
7589
|
+
* Optional so that legacy snapshots that pre-date this field continue to
|
|
7590
|
+
* parse cleanly; consumers should fall back to a 30_000ms static default
|
|
7591
|
+
* when undefined.
|
|
7592
|
+
*/
|
|
7593
|
+
equityWashTradeCooldownMs: numberType().int().nonnegative().optional(),
|
|
7594
|
+
/**
|
|
7595
|
+
* Maximum daily portfolio loss as a fraction of account equity, evaluated
|
|
7596
|
+
* intraday from the broker's `equity` vs `last_equity` delta. Breach
|
|
7597
|
+
* activates a 24h `BLOCK_NEW_OPENS` halt per Reg-T daily-loss-limit
|
|
7598
|
+
* controls. Optional so legacy snapshots parse cleanly; consumers
|
|
7599
|
+
* should fall back to the static default in `defaultRiskConfig.dailyLossLimits.maxDailyLossPercent`
|
|
7600
|
+
* (3%) when undefined. Range constrained to [0, 1) — a 100% daily-loss
|
|
7601
|
+
* threshold would be nonsensical.
|
|
7602
|
+
*/
|
|
7603
|
+
maxDailyLossPercent: numberType().min(0).max(1).optional(),
|
|
7604
|
+
macroOverlayEnabled: booleanType(),
|
|
7605
|
+
sectorOverlayEnabled: booleanType(),
|
|
7606
|
+
volatilityOverlayEnabled: booleanType(),
|
|
7607
|
+
liquidityStressOverlayEnabled: booleanType(),
|
|
7608
|
+
blackSwanProtectionEnabled: booleanType(),
|
|
7609
|
+
drawdownGuardianEnabled: booleanType(),
|
|
7610
|
+
correlationSpikeProtectionEnabled: booleanType(),
|
|
7611
|
+
newsEventRiskOverlayEnabled: booleanType(),
|
|
7612
|
+
exchangeHealthOverlayEnabled: booleanType(),
|
|
7613
|
+
dataQualitySentinelEnabled: booleanType(),
|
|
7614
|
+
miniModelProvider: nativeEnumType(LlmProvider).nullable(),
|
|
7615
|
+
miniModelId: stringType().nullable(),
|
|
7616
|
+
normalModelProvider: nativeEnumType(LlmProvider).nullable(),
|
|
7617
|
+
normalModelId: stringType().nullable(),
|
|
7618
|
+
advancedModelProvider: nativeEnumType(LlmProvider).nullable(),
|
|
7619
|
+
advancedModelId: stringType().nullable(),
|
|
7620
|
+
autonomyPrefs: AutonomyPrefsSchema,
|
|
7621
|
+
assetUniversePrefs: AssetUniversePrefsSchema,
|
|
7622
|
+
riskBudgetPrefs: RiskBudgetPrefsSchema,
|
|
7623
|
+
signalConsumptionPrefs: SignalConsumptionPrefsSchema,
|
|
7624
|
+
executionPrefs: ExecutionPrefsSchema,
|
|
7625
|
+
positionManagementPrefs: PositionManagementPrefsSchema,
|
|
7626
|
+
portfolioConstructionPrefs: PortfolioConstructionPrefsSchema,
|
|
7627
|
+
overlayResponsePrefs: OverlayResponsePrefsSchema,
|
|
7628
|
+
modelPrefs: ModelPrefsSchema,
|
|
7629
|
+
auditNotificationPrefs: AuditNotificationPrefsSchema,
|
|
7630
|
+
});
|
|
7631
|
+
|
|
7632
|
+
/**
|
|
7633
|
+
* Default trading policy used as the baseline when no user-customized policy
|
|
7634
|
+
* exists. Calibrated for short-horizon day trading / HFT-microstructuring /
|
|
7635
|
+
* scalping (intraday holds, 5-min-bar-sized stops, fast cooldowns) per the
|
|
7636
|
+
* 2026-05-10 audit, replacing the previous swing-trading calibration.
|
|
7637
|
+
*
|
|
7638
|
+
* All nested preference sub-objects are passed as empty objects so Zod applies
|
|
7639
|
+
* their field-level defaults (which are themselves tuned for scalping in their
|
|
7640
|
+
* respective schema files).
|
|
7641
|
+
*
|
|
7642
|
+
* Key choices:
|
|
7643
|
+
* - Advisory-only autonomy (no autonomous execution)
|
|
7644
|
+
* - Real-time trading enabled
|
|
7645
|
+
* - Equities, ETFs, crypto, options, futures, and forex enabled
|
|
7646
|
+
* - No shorting or margin (user must opt-in)
|
|
7647
|
+
* - All protective overlays disabled (user must opt-in)
|
|
7648
|
+
* - No LLM providers pre-configured
|
|
7649
|
+
* - Tighter per-trade allocation (2% vs 5%) and concurrency (8 positions vs 20)
|
|
7650
|
+
* reflecting the increased turnover and decreased per-position conviction
|
|
7651
|
+
* characteristic of scalping
|
|
7652
|
+
* - Faster equity wash-trade cooldown (5s vs 30s) — FINRA Rule 5210 governs
|
|
7653
|
+
* opposing-side wash trades; same-side intraday re-entry can be much faster
|
|
7654
|
+
* - Tighter daily-loss circuit breaker (2% vs 3%) reflecting that scalping
|
|
7655
|
+
* strategies should fail fast rather than burn the day's risk budget on
|
|
7656
|
+
* one bad regime
|
|
7657
|
+
*/
|
|
7658
|
+
const DEFAULT_TRADING_POLICY = EffectiveTradingPolicySchema.parse({
|
|
7659
|
+
autonomyMode: AutonomyMode.ADVISORY_ONLY,
|
|
7660
|
+
realtimeTradingEnabled: true,
|
|
7661
|
+
paperTradingOnly: false,
|
|
7662
|
+
killSwitchEnabled: false,
|
|
7663
|
+
equitiesEnabled: true,
|
|
7664
|
+
etfsEnabled: true,
|
|
7665
|
+
cryptoEnabled: true,
|
|
7666
|
+
optionsEnabled: true,
|
|
7667
|
+
futuresEnabled: true,
|
|
7668
|
+
forexEnabled: true,
|
|
7669
|
+
shortingEnabled: false,
|
|
7670
|
+
marginEnabled: false,
|
|
7671
|
+
fractionalSharesEnabled: true,
|
|
7672
|
+
maxBuyingPowerUtilPct: 90,
|
|
7673
|
+
cashFloorPct: 10,
|
|
7674
|
+
maxGrossExposurePct: 100,
|
|
7675
|
+
maxNetExposurePct: 100,
|
|
7676
|
+
maxLeverage: 1,
|
|
7677
|
+
maxSymbolConcentrationPct: 8,
|
|
7678
|
+
maxSectorConcentrationPct: 30,
|
|
7679
|
+
maxOpenPositions: 8,
|
|
7680
|
+
maxOpenOrders: 50,
|
|
7681
|
+
perTradeEquityAllocationPct: 2,
|
|
7682
|
+
perTradeCryptoAllocationPct: 2,
|
|
7683
|
+
// 5s same-side intraday re-entry cooldown for scalping. FINRA Rule 5210
|
|
7684
|
+
// governs opposing-side wash trades; same-side rapid re-entry off the
|
|
7685
|
+
// same setup is permitted within tighter bounds suited to 5-min-bar
|
|
7686
|
+
// strategies. Backend-legacy `TradingPolicy.equityWashTradeCooldownMs`
|
|
7687
|
+
// default of 30_000 ms remains the canonical row-level fallback for
|
|
7688
|
+
// accounts that have not opted into the scalping profile.
|
|
7689
|
+
equityWashTradeCooldownMs: 5_000,
|
|
7690
|
+
// 2% daily loss cap: tighter than the engine's 3% defaultRiskConfig
|
|
7691
|
+
// because scalping strategies should fail fast — three bad regimes at 3%
|
|
7692
|
+
// each is a 9% intraday burn before the kill-switch fires.
|
|
7693
|
+
maxDailyLossPercent: 0.02,
|
|
7694
|
+
macroOverlayEnabled: false,
|
|
7695
|
+
sectorOverlayEnabled: false,
|
|
7696
|
+
volatilityOverlayEnabled: false,
|
|
7697
|
+
liquidityStressOverlayEnabled: false,
|
|
7698
|
+
blackSwanProtectionEnabled: false,
|
|
7699
|
+
drawdownGuardianEnabled: false,
|
|
7700
|
+
correlationSpikeProtectionEnabled: false,
|
|
7701
|
+
newsEventRiskOverlayEnabled: false,
|
|
7702
|
+
exchangeHealthOverlayEnabled: false,
|
|
7703
|
+
dataQualitySentinelEnabled: false,
|
|
7704
|
+
miniModelProvider: null,
|
|
7705
|
+
miniModelId: null,
|
|
7706
|
+
normalModelProvider: null,
|
|
7707
|
+
normalModelId: null,
|
|
7708
|
+
advancedModelProvider: null,
|
|
7709
|
+
advancedModelId: null,
|
|
7710
|
+
autonomyPrefs: {},
|
|
7711
|
+
assetUniversePrefs: {},
|
|
7712
|
+
riskBudgetPrefs: {},
|
|
7713
|
+
signalConsumptionPrefs: {},
|
|
7714
|
+
executionPrefs: {},
|
|
7715
|
+
positionManagementPrefs: {},
|
|
7716
|
+
portfolioConstructionPrefs: {},
|
|
7717
|
+
overlayResponsePrefs: {},
|
|
7718
|
+
modelPrefs: {},
|
|
7719
|
+
auditNotificationPrefs: {},
|
|
7720
|
+
});
|
|
7721
|
+
|
|
3053
7722
|
const log = (message) => {
|
|
3054
7723
|
console.log(`[${new Date().toLocaleString("en-US", { timeZone: "America/New_York" })}] ${message}`);
|
|
3055
7724
|
};
|
|
7725
|
+
/**
|
|
7726
|
+
* Lock-in test for the scalping-tuned defaults landed by the 2026-05-10 audit.
|
|
7727
|
+
* Re-parses an empty policy and asserts the new defaults hold for every field
|
|
7728
|
+
* that was tightened. Prints the full parsed object so any future drift is
|
|
7729
|
+
* immediately visible in test output. Throws on the first mismatch so this
|
|
7730
|
+
* surfaces as a non-zero exit code under `npm run test:legacy`.
|
|
7731
|
+
*/
|
|
7732
|
+
function testScalpingDefaultsLockedIn() {
|
|
7733
|
+
log("Verifying DEFAULT_TRADING_POLICY scalping calibration...");
|
|
7734
|
+
const policy = EffectiveTradingPolicySchema.parse({});
|
|
7735
|
+
const expectations = [
|
|
7736
|
+
// Top-level scalars
|
|
7737
|
+
["maxOpenPositions", policy.maxOpenPositions, 8],
|
|
7738
|
+
["maxSymbolConcentrationPct", policy.maxSymbolConcentrationPct, 8],
|
|
7739
|
+
["equityWashTradeCooldownMs", policy.equityWashTradeCooldownMs, 5_000],
|
|
7740
|
+
["maxDailyLossPercent", policy.maxDailyLossPercent, 0.02],
|
|
7741
|
+
[
|
|
7742
|
+
"perTradeEquityAllocationPct",
|
|
7743
|
+
policy.perTradeEquityAllocationPct,
|
|
7744
|
+
2,
|
|
7745
|
+
],
|
|
7746
|
+
[
|
|
7747
|
+
"perTradeCryptoAllocationPct",
|
|
7748
|
+
policy.perTradeCryptoAllocationPct,
|
|
7749
|
+
2,
|
|
7750
|
+
],
|
|
7751
|
+
// positionManagementPrefs
|
|
7752
|
+
[
|
|
7753
|
+
"positionManagementPrefs.defaultStopLossPct",
|
|
7754
|
+
policy.positionManagementPrefs.defaultStopLossPct,
|
|
7755
|
+
0.5,
|
|
7756
|
+
],
|
|
7757
|
+
[
|
|
7758
|
+
"positionManagementPrefs.defaultTakeProfitPct",
|
|
7759
|
+
policy.positionManagementPrefs.defaultTakeProfitPct,
|
|
7760
|
+
1.0,
|
|
7761
|
+
],
|
|
7762
|
+
[
|
|
7763
|
+
"positionManagementPrefs.atrStopMultiplier",
|
|
7764
|
+
policy.positionManagementPrefs.atrStopMultiplier,
|
|
7765
|
+
0.75,
|
|
7766
|
+
],
|
|
7767
|
+
[
|
|
7768
|
+
"positionManagementPrefs.maxHoldingPeriodMinutes",
|
|
7769
|
+
policy.positionManagementPrefs.maxHoldingPeriodMinutes,
|
|
7770
|
+
30,
|
|
7771
|
+
],
|
|
7772
|
+
[
|
|
7773
|
+
"positionManagementPrefs.doNotReenterAfterStopOutMinutes",
|
|
7774
|
+
policy.positionManagementPrefs.doNotReenterAfterStopOutMinutes,
|
|
7775
|
+
10,
|
|
7776
|
+
],
|
|
7777
|
+
[
|
|
7778
|
+
"positionManagementPrefs.scaleOutTriggerPct",
|
|
7779
|
+
policy.positionManagementPrefs.scaleOutTriggerPct,
|
|
7780
|
+
0.5,
|
|
7781
|
+
],
|
|
7782
|
+
[
|
|
7783
|
+
"positionManagementPrefs.defaultRiskRewardRatio",
|
|
7784
|
+
policy.positionManagementPrefs.defaultRiskRewardRatio,
|
|
7785
|
+
1.5,
|
|
7786
|
+
],
|
|
7787
|
+
[
|
|
7788
|
+
"positionManagementPrefs.breakEvenStopEnabled",
|
|
7789
|
+
policy.positionManagementPrefs.breakEvenStopEnabled,
|
|
7790
|
+
true,
|
|
7791
|
+
],
|
|
7792
|
+
[
|
|
7793
|
+
"positionManagementPrefs.breakEvenTriggerPct",
|
|
7794
|
+
policy.positionManagementPrefs.breakEvenTriggerPct,
|
|
7795
|
+
1,
|
|
7796
|
+
],
|
|
7797
|
+
// signalConsumptionPrefs
|
|
7798
|
+
[
|
|
7799
|
+
"signalConsumptionPrefs.minConfidenceByDefault",
|
|
7800
|
+
policy.signalConsumptionPrefs.minConfidenceByDefault,
|
|
7801
|
+
65,
|
|
7802
|
+
],
|
|
7803
|
+
[
|
|
7804
|
+
"signalConsumptionPrefs.minPercentageChange",
|
|
7805
|
+
policy.signalConsumptionPrefs.minPercentageChange,
|
|
7806
|
+
0.15,
|
|
7807
|
+
],
|
|
7808
|
+
[
|
|
7809
|
+
"signalConsumptionPrefs.volumeThreshold",
|
|
7810
|
+
policy.signalConsumptionPrefs.volumeThreshold,
|
|
7811
|
+
100_000,
|
|
7812
|
+
],
|
|
7813
|
+
[
|
|
7814
|
+
"signalConsumptionPrefs.minExpectedRewardRiskRatio",
|
|
7815
|
+
policy.signalConsumptionPrefs.minExpectedRewardRiskRatio,
|
|
7816
|
+
1.3,
|
|
7817
|
+
],
|
|
7818
|
+
[
|
|
7819
|
+
"signalConsumptionPrefs.cooldownAfterEntrySeconds",
|
|
7820
|
+
policy.signalConsumptionPrefs.cooldownAfterEntrySeconds,
|
|
7821
|
+
5,
|
|
7822
|
+
],
|
|
7823
|
+
[
|
|
7824
|
+
"signalConsumptionPrefs.cooldownAfterStopOutSeconds",
|
|
7825
|
+
policy.signalConsumptionPrefs.cooldownAfterStopOutSeconds,
|
|
7826
|
+
30,
|
|
7827
|
+
],
|
|
7828
|
+
[
|
|
7829
|
+
"signalConsumptionPrefs.duplicateSignalSuppressionWindowSeconds",
|
|
7830
|
+
policy.signalConsumptionPrefs.duplicateSignalSuppressionWindowSeconds,
|
|
7831
|
+
10,
|
|
7832
|
+
],
|
|
7833
|
+
[
|
|
7834
|
+
"signalConsumptionPrefs.maxSignalAgeSeconds",
|
|
7835
|
+
policy.signalConsumptionPrefs.maxSignalAgeSeconds,
|
|
7836
|
+
30,
|
|
7837
|
+
],
|
|
7838
|
+
// riskBudgetPrefs
|
|
7839
|
+
[
|
|
7840
|
+
"riskBudgetPrefs.maxRiskPerTradePct",
|
|
7841
|
+
policy.riskBudgetPrefs.maxRiskPerTradePct,
|
|
7842
|
+
1.5,
|
|
7843
|
+
],
|
|
7844
|
+
[
|
|
7845
|
+
"riskBudgetPrefs.maxLossPerDayPct",
|
|
7846
|
+
policy.riskBudgetPrefs.maxLossPerDayPct,
|
|
7847
|
+
2.0,
|
|
7848
|
+
],
|
|
7849
|
+
[
|
|
7850
|
+
"riskBudgetPrefs.maxLossPerWeekPct",
|
|
7851
|
+
policy.riskBudgetPrefs.maxLossPerWeekPct,
|
|
7852
|
+
5.0,
|
|
7853
|
+
],
|
|
7854
|
+
[
|
|
7855
|
+
"riskBudgetPrefs.perTradeAllocationPct",
|
|
7856
|
+
policy.riskBudgetPrefs.perTradeAllocationPct,
|
|
7857
|
+
2,
|
|
7858
|
+
],
|
|
7859
|
+
// executionPrefs
|
|
7860
|
+
[
|
|
7861
|
+
"executionPrefs.maxSlippageTolerancePct",
|
|
7862
|
+
policy.executionPrefs.maxSlippageTolerancePct,
|
|
7863
|
+
0.3,
|
|
7864
|
+
],
|
|
7865
|
+
[
|
|
7866
|
+
"executionPrefs.priceCollarPct",
|
|
7867
|
+
policy.executionPrefs.priceCollarPct,
|
|
7868
|
+
0.5,
|
|
7869
|
+
],
|
|
7870
|
+
[
|
|
7871
|
+
"executionPrefs.executionBias",
|
|
7872
|
+
policy.executionPrefs.executionBias,
|
|
7873
|
+
"passive",
|
|
7874
|
+
],
|
|
7875
|
+
[
|
|
7876
|
+
"executionPrefs.defaultTimeInForce",
|
|
7877
|
+
policy.executionPrefs.defaultTimeInForce,
|
|
7878
|
+
"ioc",
|
|
7879
|
+
],
|
|
7880
|
+
[
|
|
7881
|
+
"executionPrefs.preferredOrderType",
|
|
7882
|
+
policy.executionPrefs.preferredOrderType,
|
|
7883
|
+
"limit",
|
|
7884
|
+
],
|
|
7885
|
+
[
|
|
7886
|
+
"executionPrefs.partialFillPolicy",
|
|
7887
|
+
policy.executionPrefs.partialFillPolicy,
|
|
7888
|
+
"accept_partial",
|
|
7889
|
+
],
|
|
7890
|
+
];
|
|
7891
|
+
let mismatches = 0;
|
|
7892
|
+
for (const [path, actual, expected] of expectations) {
|
|
7893
|
+
if (actual !== expected) {
|
|
7894
|
+
mismatches += 1;
|
|
7895
|
+
log(`MISMATCH: ${path} = ${JSON.stringify(actual)} (expected ${JSON.stringify(expected)})`);
|
|
7896
|
+
}
|
|
7897
|
+
}
|
|
7898
|
+
if (mismatches > 0) {
|
|
7899
|
+
log(`${mismatches} default(s) drifted from scalping calibration.`);
|
|
7900
|
+
log("Full parsed policy follows for debugging:");
|
|
7901
|
+
console.log(JSON.stringify(policy, null, 2));
|
|
7902
|
+
throw new Error(`Scalping default lock-in failed: ${mismatches} mismatches.`);
|
|
7903
|
+
}
|
|
7904
|
+
// Also confirm DEFAULT_TRADING_POLICY (which is the parsed object built
|
|
7905
|
+
// with explicit top-level scalars) agrees with the schema-derived policy
|
|
7906
|
+
// on every nested field that came from defaults — this catches any case
|
|
7907
|
+
// where someone passes a stale literal in default-trading-policy.ts that
|
|
7908
|
+
// overrides what the schema is now producing.
|
|
7909
|
+
if (DEFAULT_TRADING_POLICY.maxOpenPositions !== 8 ||
|
|
7910
|
+
DEFAULT_TRADING_POLICY.equityWashTradeCooldownMs !== 5_000 ||
|
|
7911
|
+
DEFAULT_TRADING_POLICY.maxDailyLossPercent !== 0.02) {
|
|
7912
|
+
throw new Error("DEFAULT_TRADING_POLICY top-level scalars drifted from scalping calibration.");
|
|
7913
|
+
}
|
|
7914
|
+
log(`Scalping defaults locked in (${expectations.length} fields verified). Full policy:`);
|
|
7915
|
+
console.log(JSON.stringify(policy, null, 2));
|
|
7916
|
+
}
|
|
3056
7917
|
// async function testCreateEquitiesTrade() {
|
|
3057
7918
|
// try {
|
|
3058
7919
|
// log('Starting createEquitiesTrade test...');
|
|
@@ -3314,5 +8175,7 @@ async function testPreMarketData() {
|
|
|
3314
8175
|
log(`❌ Error in testPreMarketData: ${error instanceof Error ? error.message : error}`);
|
|
3315
8176
|
}
|
|
3316
8177
|
}
|
|
8178
|
+
// Lock-in scalping defaults first so any drift fails fast with exit 1.
|
|
8179
|
+
testScalpingDefaultsLockedIn();
|
|
3317
8180
|
testPreMarketData();
|
|
3318
8181
|
//# sourceMappingURL=test.js.map
|