| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- import * as checks from "./checks.js";
- import * as registries from "./registries.js";
- import * as schemas from "./schemas.js";
- import * as util from "./util.js";
- // @__NO_SIDE_EFFECTS__
- export function _string(Class, params) {
- return new Class({
- type: "string",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _coercedString(Class, params) {
- return new Class({
- type: "string",
- coerce: true,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _email(Class, params) {
- return new Class({
- type: "string",
- format: "email",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _guid(Class, params) {
- return new Class({
- type: "string",
- format: "guid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uuid(Class, params) {
- return new Class({
- type: "string",
- format: "uuid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uuidv4(Class, params) {
- return new Class({
- type: "string",
- format: "uuid",
- check: "string_format",
- abort: false,
- version: "v4",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uuidv6(Class, params) {
- return new Class({
- type: "string",
- format: "uuid",
- check: "string_format",
- abort: false,
- version: "v6",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uuidv7(Class, params) {
- return new Class({
- type: "string",
- format: "uuid",
- check: "string_format",
- abort: false,
- version: "v7",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _url(Class, params) {
- return new Class({
- type: "string",
- format: "url",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _emoji(Class, params) {
- return new Class({
- type: "string",
- format: "emoji",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _nanoid(Class, params) {
- return new Class({
- type: "string",
- format: "nanoid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _cuid(Class, params) {
- return new Class({
- type: "string",
- format: "cuid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _cuid2(Class, params) {
- return new Class({
- type: "string",
- format: "cuid2",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _ulid(Class, params) {
- return new Class({
- type: "string",
- format: "ulid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _xid(Class, params) {
- return new Class({
- type: "string",
- format: "xid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _ksuid(Class, params) {
- return new Class({
- type: "string",
- format: "ksuid",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _ipv4(Class, params) {
- return new Class({
- type: "string",
- format: "ipv4",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _ipv6(Class, params) {
- return new Class({
- type: "string",
- format: "ipv6",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _mac(Class, params) {
- return new Class({
- type: "string",
- format: "mac",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _cidrv4(Class, params) {
- return new Class({
- type: "string",
- format: "cidrv4",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _cidrv6(Class, params) {
- return new Class({
- type: "string",
- format: "cidrv6",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _base64(Class, params) {
- return new Class({
- type: "string",
- format: "base64",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _base64url(Class, params) {
- return new Class({
- type: "string",
- format: "base64url",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _e164(Class, params) {
- return new Class({
- type: "string",
- format: "e164",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _jwt(Class, params) {
- return new Class({
- type: "string",
- format: "jwt",
- check: "string_format",
- abort: false,
- ...util.normalizeParams(params),
- });
- }
- export const TimePrecision = {
- Any: null,
- Minute: -1,
- Second: 0,
- Millisecond: 3,
- Microsecond: 6,
- };
- // @__NO_SIDE_EFFECTS__
- export function _isoDateTime(Class, params) {
- return new Class({
- type: "string",
- format: "datetime",
- check: "string_format",
- offset: false,
- local: false,
- precision: null,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _isoDate(Class, params) {
- return new Class({
- type: "string",
- format: "date",
- check: "string_format",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _isoTime(Class, params) {
- return new Class({
- type: "string",
- format: "time",
- check: "string_format",
- precision: null,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _isoDuration(Class, params) {
- return new Class({
- type: "string",
- format: "duration",
- check: "string_format",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _number(Class, params) {
- return new Class({
- type: "number",
- checks: [],
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _coercedNumber(Class, params) {
- return new Class({
- type: "number",
- coerce: true,
- checks: [],
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _int(Class, params) {
- return new Class({
- type: "number",
- check: "number_format",
- abort: false,
- format: "safeint",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _float32(Class, params) {
- return new Class({
- type: "number",
- check: "number_format",
- abort: false,
- format: "float32",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _float64(Class, params) {
- return new Class({
- type: "number",
- check: "number_format",
- abort: false,
- format: "float64",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _int32(Class, params) {
- return new Class({
- type: "number",
- check: "number_format",
- abort: false,
- format: "int32",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uint32(Class, params) {
- return new Class({
- type: "number",
- check: "number_format",
- abort: false,
- format: "uint32",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _boolean(Class, params) {
- return new Class({
- type: "boolean",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _coercedBoolean(Class, params) {
- return new Class({
- type: "boolean",
- coerce: true,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _bigint(Class, params) {
- return new Class({
- type: "bigint",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _coercedBigint(Class, params) {
- return new Class({
- type: "bigint",
- coerce: true,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _int64(Class, params) {
- return new Class({
- type: "bigint",
- check: "bigint_format",
- abort: false,
- format: "int64",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uint64(Class, params) {
- return new Class({
- type: "bigint",
- check: "bigint_format",
- abort: false,
- format: "uint64",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _symbol(Class, params) {
- return new Class({
- type: "symbol",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _undefined(Class, params) {
- return new Class({
- type: "undefined",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _null(Class, params) {
- return new Class({
- type: "null",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _any(Class) {
- return new Class({
- type: "any",
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _unknown(Class) {
- return new Class({
- type: "unknown",
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _never(Class, params) {
- return new Class({
- type: "never",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _void(Class, params) {
- return new Class({
- type: "void",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _date(Class, params) {
- return new Class({
- type: "date",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _coercedDate(Class, params) {
- return new Class({
- type: "date",
- coerce: true,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _nan(Class, params) {
- return new Class({
- type: "nan",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _lt(value, params) {
- return new checks.$ZodCheckLessThan({
- check: "less_than",
- ...util.normalizeParams(params),
- value,
- inclusive: false,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _lte(value, params) {
- return new checks.$ZodCheckLessThan({
- check: "less_than",
- ...util.normalizeParams(params),
- value,
- inclusive: true,
- });
- }
- export {
- /** @deprecated Use `z.lte()` instead. */
- _lte as _max, };
- // @__NO_SIDE_EFFECTS__
- export function _gt(value, params) {
- return new checks.$ZodCheckGreaterThan({
- check: "greater_than",
- ...util.normalizeParams(params),
- value,
- inclusive: false,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _gte(value, params) {
- return new checks.$ZodCheckGreaterThan({
- check: "greater_than",
- ...util.normalizeParams(params),
- value,
- inclusive: true,
- });
- }
- export {
- /** @deprecated Use `z.gte()` instead. */
- _gte as _min, };
- // @__NO_SIDE_EFFECTS__
- export function _positive(params) {
- return _gt(0, params);
- }
- // negative
- // @__NO_SIDE_EFFECTS__
- export function _negative(params) {
- return _lt(0, params);
- }
- // nonpositive
- // @__NO_SIDE_EFFECTS__
- export function _nonpositive(params) {
- return _lte(0, params);
- }
- // nonnegative
- // @__NO_SIDE_EFFECTS__
- export function _nonnegative(params) {
- return _gte(0, params);
- }
- // @__NO_SIDE_EFFECTS__
- export function _multipleOf(value, params) {
- return new checks.$ZodCheckMultipleOf({
- check: "multiple_of",
- ...util.normalizeParams(params),
- value,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _maxSize(maximum, params) {
- return new checks.$ZodCheckMaxSize({
- check: "max_size",
- ...util.normalizeParams(params),
- maximum,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _minSize(minimum, params) {
- return new checks.$ZodCheckMinSize({
- check: "min_size",
- ...util.normalizeParams(params),
- minimum,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _size(size, params) {
- return new checks.$ZodCheckSizeEquals({
- check: "size_equals",
- ...util.normalizeParams(params),
- size,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _maxLength(maximum, params) {
- const ch = new checks.$ZodCheckMaxLength({
- check: "max_length",
- ...util.normalizeParams(params),
- maximum,
- });
- return ch;
- }
- // @__NO_SIDE_EFFECTS__
- export function _minLength(minimum, params) {
- return new checks.$ZodCheckMinLength({
- check: "min_length",
- ...util.normalizeParams(params),
- minimum,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _length(length, params) {
- return new checks.$ZodCheckLengthEquals({
- check: "length_equals",
- ...util.normalizeParams(params),
- length,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _regex(pattern, params) {
- return new checks.$ZodCheckRegex({
- check: "string_format",
- format: "regex",
- ...util.normalizeParams(params),
- pattern,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _lowercase(params) {
- return new checks.$ZodCheckLowerCase({
- check: "string_format",
- format: "lowercase",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _uppercase(params) {
- return new checks.$ZodCheckUpperCase({
- check: "string_format",
- format: "uppercase",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _includes(includes, params) {
- return new checks.$ZodCheckIncludes({
- check: "string_format",
- format: "includes",
- ...util.normalizeParams(params),
- includes,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _startsWith(prefix, params) {
- return new checks.$ZodCheckStartsWith({
- check: "string_format",
- format: "starts_with",
- ...util.normalizeParams(params),
- prefix,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _endsWith(suffix, params) {
- return new checks.$ZodCheckEndsWith({
- check: "string_format",
- format: "ends_with",
- ...util.normalizeParams(params),
- suffix,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _property(property, schema, params) {
- return new checks.$ZodCheckProperty({
- check: "property",
- property,
- schema,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _mime(types, params) {
- return new checks.$ZodCheckMimeType({
- check: "mime_type",
- mime: types,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _overwrite(tx) {
- return new checks.$ZodCheckOverwrite({
- check: "overwrite",
- tx,
- });
- }
- // normalize
- // @__NO_SIDE_EFFECTS__
- export function _normalize(form) {
- return _overwrite((input) => input.normalize(form));
- }
- // trim
- // @__NO_SIDE_EFFECTS__
- export function _trim() {
- return _overwrite((input) => input.trim());
- }
- // toLowerCase
- // @__NO_SIDE_EFFECTS__
- export function _toLowerCase() {
- return _overwrite((input) => input.toLowerCase());
- }
- // toUpperCase
- // @__NO_SIDE_EFFECTS__
- export function _toUpperCase() {
- return _overwrite((input) => input.toUpperCase());
- }
- // slugify
- // @__NO_SIDE_EFFECTS__
- export function _slugify() {
- return _overwrite((input) => util.slugify(input));
- }
- // @__NO_SIDE_EFFECTS__
- export function _array(Class, element, params) {
- return new Class({
- type: "array",
- element,
- // get element() {
- // return element;
- // },
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _union(Class, options, params) {
- return new Class({
- type: "union",
- options,
- ...util.normalizeParams(params),
- });
- }
- export function _xor(Class, options, params) {
- return new Class({
- type: "union",
- options,
- inclusive: false,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _discriminatedUnion(Class, discriminator, options, params) {
- return new Class({
- type: "union",
- options,
- discriminator,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _intersection(Class, left, right) {
- return new Class({
- type: "intersection",
- left,
- right,
- });
- }
- // export function _tuple(
- // Class: util.SchemaClass<schemas.$ZodTuple>,
- // items: [],
- // params?: string | $ZodTupleParams
- // ): schemas.$ZodTuple<[], null>;
- // @__NO_SIDE_EFFECTS__
- export function _tuple(Class, items, _paramsOrRest, _params) {
- const hasRest = _paramsOrRest instanceof schemas.$ZodType;
- const params = hasRest ? _params : _paramsOrRest;
- const rest = hasRest ? _paramsOrRest : null;
- return new Class({
- type: "tuple",
- items,
- rest,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _record(Class, keyType, valueType, params) {
- return new Class({
- type: "record",
- keyType,
- valueType,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _map(Class, keyType, valueType, params) {
- return new Class({
- type: "map",
- keyType,
- valueType,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _set(Class, valueType, params) {
- return new Class({
- type: "set",
- valueType,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _enum(Class, values, params) {
- const entries = Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values;
- // if (Array.isArray(values)) {
- // for (const value of values) {
- // entries[value] = value;
- // }
- // } else {
- // Object.assign(entries, values);
- // }
- // const entries: util.EnumLike = {};
- // for (const val of values) {
- // entries[val] = val;
- // }
- return new Class({
- type: "enum",
- entries,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- /** @deprecated This API has been merged into `z.enum()`. Use `z.enum()` instead.
- *
- * ```ts
- * enum Colors { red, green, blue }
- * z.enum(Colors);
- * ```
- */
- export function _nativeEnum(Class, entries, params) {
- return new Class({
- type: "enum",
- entries,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _literal(Class, value, params) {
- return new Class({
- type: "literal",
- values: Array.isArray(value) ? value : [value],
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _file(Class, params) {
- return new Class({
- type: "file",
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _transform(Class, fn) {
- return new Class({
- type: "transform",
- transform: fn,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _optional(Class, innerType) {
- return new Class({
- type: "optional",
- innerType,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _nullable(Class, innerType) {
- return new Class({
- type: "nullable",
- innerType,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _default(Class, innerType, defaultValue) {
- return new Class({
- type: "default",
- innerType,
- get defaultValue() {
- return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
- },
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _nonoptional(Class, innerType, params) {
- return new Class({
- type: "nonoptional",
- innerType,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _success(Class, innerType) {
- return new Class({
- type: "success",
- innerType,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _catch(Class, innerType, catchValue) {
- return new Class({
- type: "catch",
- innerType,
- catchValue: (typeof catchValue === "function" ? catchValue : () => catchValue),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _pipe(Class, in_, out) {
- return new Class({
- type: "pipe",
- in: in_,
- out,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _readonly(Class, innerType) {
- return new Class({
- type: "readonly",
- innerType,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _templateLiteral(Class, parts, params) {
- return new Class({
- type: "template_literal",
- parts,
- ...util.normalizeParams(params),
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _lazy(Class, getter) {
- return new Class({
- type: "lazy",
- getter,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _promise(Class, innerType) {
- return new Class({
- type: "promise",
- innerType,
- });
- }
- // @__NO_SIDE_EFFECTS__
- export function _custom(Class, fn, _params) {
- const norm = util.normalizeParams(_params);
- norm.abort ?? (norm.abort = true); // default to abort:false
- const schema = new Class({
- type: "custom",
- check: "custom",
- fn: fn,
- ...norm,
- });
- return schema;
- }
- // same as _custom but defaults to abort:false
- // @__NO_SIDE_EFFECTS__
- export function _refine(Class, fn, _params) {
- const schema = new Class({
- type: "custom",
- check: "custom",
- fn: fn,
- ...util.normalizeParams(_params),
- });
- return schema;
- }
- // @__NO_SIDE_EFFECTS__
- export function _superRefine(fn) {
- const ch = _check((payload) => {
- payload.addIssue = (issue) => {
- if (typeof issue === "string") {
- payload.issues.push(util.issue(issue, payload.value, ch._zod.def));
- }
- else {
- // for Zod 3 backwards compatibility
- const _issue = issue;
- if (_issue.fatal)
- _issue.continue = false;
- _issue.code ?? (_issue.code = "custom");
- _issue.input ?? (_issue.input = payload.value);
- _issue.inst ?? (_issue.inst = ch);
- _issue.continue ?? (_issue.continue = !ch._zod.def.abort); // abort is always undefined, so this is always true...
- payload.issues.push(util.issue(_issue));
- }
- };
- return fn(payload.value, payload);
- });
- return ch;
- }
- // @__NO_SIDE_EFFECTS__
- export function _check(fn, params) {
- const ch = new checks.$ZodCheck({
- check: "custom",
- ...util.normalizeParams(params),
- });
- ch._zod.check = fn;
- return ch;
- }
- // @__NO_SIDE_EFFECTS__
- export function describe(description) {
- const ch = new checks.$ZodCheck({ check: "describe" });
- ch._zod.onattach = [
- (inst) => {
- const existing = registries.globalRegistry.get(inst) ?? {};
- registries.globalRegistry.add(inst, { ...existing, description });
- },
- ];
- ch._zod.check = () => { }; // no-op check
- return ch;
- }
- // @__NO_SIDE_EFFECTS__
- export function meta(metadata) {
- const ch = new checks.$ZodCheck({ check: "meta" });
- ch._zod.onattach = [
- (inst) => {
- const existing = registries.globalRegistry.get(inst) ?? {};
- registries.globalRegistry.add(inst, { ...existing, ...metadata });
- },
- ];
- ch._zod.check = () => { }; // no-op check
- return ch;
- }
- // @__NO_SIDE_EFFECTS__
- export function _stringbool(Classes, _params) {
- const params = util.normalizeParams(_params);
- let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
- let falsyArray = params.falsy ?? ["false", "0", "no", "off", "n", "disabled"];
- if (params.case !== "sensitive") {
- truthyArray = truthyArray.map((v) => (typeof v === "string" ? v.toLowerCase() : v));
- falsyArray = falsyArray.map((v) => (typeof v === "string" ? v.toLowerCase() : v));
- }
- const truthySet = new Set(truthyArray);
- const falsySet = new Set(falsyArray);
- const _Codec = Classes.Codec ?? schemas.$ZodCodec;
- const _Boolean = Classes.Boolean ?? schemas.$ZodBoolean;
- const _String = Classes.String ?? schemas.$ZodString;
- const stringSchema = new _String({ type: "string", error: params.error });
- const booleanSchema = new _Boolean({ type: "boolean", error: params.error });
- const codec = new _Codec({
- type: "pipe",
- in: stringSchema,
- out: booleanSchema,
- transform: ((input, payload) => {
- let data = input;
- if (params.case !== "sensitive")
- data = data.toLowerCase();
- if (truthySet.has(data)) {
- return true;
- }
- else if (falsySet.has(data)) {
- return false;
- }
- else {
- payload.issues.push({
- code: "invalid_value",
- expected: "stringbool",
- values: [...truthySet, ...falsySet],
- input: payload.value,
- inst: codec,
- continue: false,
- });
- return {};
- }
- }),
- reverseTransform: ((input, _payload) => {
- if (input === true) {
- return truthyArray[0] || "true";
- }
- else {
- return falsyArray[0] || "false";
- }
- }),
- error: params.error,
- });
- return codec;
- }
- // @__NO_SIDE_EFFECTS__
- export function _stringFormat(Class, format, fnOrRegex, _params = {}) {
- const params = util.normalizeParams(_params);
- const def = {
- ...util.normalizeParams(_params),
- check: "string_format",
- type: "string",
- format,
- fn: typeof fnOrRegex === "function" ? fnOrRegex : (val) => fnOrRegex.test(val),
- ...params,
- };
- if (fnOrRegex instanceof RegExp) {
- def.pattern = fnOrRegex;
- }
- const inst = new Class(def);
- return inst;
- }
|