@vielzeug/spell 26.11.9 → 26.11.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/_dev.cjs.map +1 -1
- package/dist/_dev.js.map +1 -1
- package/dist/_object.cjs +2 -0
- package/dist/_object.cjs.map +1 -0
- package/dist/_object.d.ts +9 -0
- package/dist/_object.d.ts.map +1 -0
- package/dist/_object.js +2 -0
- package/dist/_object.js.map +1 -0
- package/dist/_parse.cjs +2 -0
- package/dist/_parse.cjs.map +1 -0
- package/dist/_parse.d.ts +38 -0
- package/dist/_parse.d.ts.map +1 -0
- package/dist/_parse.js +2 -0
- package/dist/_parse.js.map +1 -0
- package/dist/errors.cjs +2 -2
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +15 -26
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/json.cjs +2 -1
- package/dist/json.cjs.map +1 -0
- package/dist/json.d.ts +8 -1
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js +2 -1
- package/dist/json.js.map +1 -0
- package/dist/messages.cjs +1 -1
- package/dist/messages.cjs.map +1 -1
- package/dist/messages.d.ts +7 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +1 -1
- package/dist/messages.js.map +1 -1
- package/dist/predicates.cjs +2 -1
- package/dist/predicates.cjs.map +1 -0
- package/dist/predicates.d.ts +41 -2
- package/dist/predicates.d.ts.map +1 -1
- package/dist/predicates.js +2 -1
- package/dist/predicates.js.map +1 -0
- package/dist/s.cjs +1 -1
- package/dist/s.cjs.map +1 -1
- package/dist/s.d.ts +21 -22
- package/dist/s.d.ts.map +1 -1
- package/dist/s.js +1 -1
- package/dist/s.js.map +1 -1
- package/dist/schema.cjs +2 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.ts +93 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -0
- package/dist/schemas/array.cjs +1 -1
- package/dist/schemas/array.cjs.map +1 -1
- package/dist/schemas/array.d.ts +22 -32
- package/dist/schemas/array.d.ts.map +1 -1
- package/dist/schemas/array.js +1 -1
- package/dist/schemas/array.js.map +1 -1
- package/dist/schemas/bigint.cjs +1 -1
- package/dist/schemas/bigint.cjs.map +1 -1
- package/dist/schemas/bigint.d.ts +14 -18
- package/dist/schemas/bigint.d.ts.map +1 -1
- package/dist/schemas/bigint.js +1 -1
- package/dist/schemas/bigint.js.map +1 -1
- package/dist/schemas/date.cjs +1 -1
- package/dist/schemas/date.cjs.map +1 -1
- package/dist/schemas/date.d.ts +7 -6
- package/dist/schemas/date.d.ts.map +1 -1
- package/dist/schemas/date.js +1 -1
- package/dist/schemas/date.js.map +1 -1
- package/dist/schemas/discriminated-union.cjs +2 -0
- package/dist/schemas/discriminated-union.cjs.map +1 -0
- package/dist/schemas/discriminated-union.d.ts +39 -0
- package/dist/schemas/discriminated-union.d.ts.map +1 -0
- package/dist/schemas/discriminated-union.js +2 -0
- package/dist/schemas/discriminated-union.js.map +1 -0
- package/dist/schemas/intersect.cjs +1 -1
- package/dist/schemas/intersect.cjs.map +1 -1
- package/dist/schemas/intersect.d.ts +10 -9
- package/dist/schemas/intersect.d.ts.map +1 -1
- package/dist/schemas/intersect.js +1 -1
- package/dist/schemas/intersect.js.map +1 -1
- package/dist/schemas/lazy.cjs +1 -1
- package/dist/schemas/lazy.cjs.map +1 -1
- package/dist/schemas/lazy.d.ts +10 -10
- package/dist/schemas/lazy.d.ts.map +1 -1
- package/dist/schemas/lazy.js +1 -1
- package/dist/schemas/lazy.js.map +1 -1
- package/dist/schemas/map.cjs +1 -1
- package/dist/schemas/map.cjs.map +1 -1
- package/dist/schemas/map.d.ts +8 -7
- package/dist/schemas/map.d.ts.map +1 -1
- package/dist/schemas/map.js +1 -1
- package/dist/schemas/map.js.map +1 -1
- package/dist/schemas/number.cjs +1 -1
- package/dist/schemas/number.cjs.map +1 -1
- package/dist/schemas/number.d.ts +20 -27
- package/dist/schemas/number.d.ts.map +1 -1
- package/dist/schemas/number.js +1 -1
- package/dist/schemas/number.js.map +1 -1
- package/dist/schemas/object.cjs +1 -1
- package/dist/schemas/object.cjs.map +1 -1
- package/dist/schemas/object.d.ts +40 -88
- package/dist/schemas/object.d.ts.map +1 -1
- package/dist/schemas/object.js +1 -1
- package/dist/schemas/object.js.map +1 -1
- package/dist/schemas/primitives.cjs +2 -0
- package/dist/schemas/primitives.cjs.map +1 -0
- package/dist/schemas/primitives.d.ts +38 -0
- package/dist/schemas/primitives.d.ts.map +1 -0
- package/dist/schemas/primitives.js +2 -0
- package/dist/schemas/primitives.js.map +1 -0
- package/dist/schemas/record.cjs +1 -1
- package/dist/schemas/record.cjs.map +1 -1
- package/dist/schemas/record.d.ts +7 -8
- package/dist/schemas/record.d.ts.map +1 -1
- package/dist/schemas/record.js +1 -1
- package/dist/schemas/record.js.map +1 -1
- package/dist/schemas/set.cjs +1 -1
- package/dist/schemas/set.cjs.map +1 -1
- package/dist/schemas/set.d.ts +8 -7
- package/dist/schemas/set.d.ts.map +1 -1
- package/dist/schemas/set.js +1 -1
- package/dist/schemas/set.js.map +1 -1
- package/dist/schemas/string.cjs +1 -1
- package/dist/schemas/string.cjs.map +1 -1
- package/dist/schemas/string.d.ts +42 -98
- package/dist/schemas/string.d.ts.map +1 -1
- package/dist/schemas/string.js +1 -1
- package/dist/schemas/string.js.map +1 -1
- package/dist/schemas/tuple.cjs +1 -1
- package/dist/schemas/tuple.cjs.map +1 -1
- package/dist/schemas/tuple.d.ts +9 -9
- package/dist/schemas/tuple.d.ts.map +1 -1
- package/dist/schemas/tuple.js +1 -1
- package/dist/schemas/tuple.js.map +1 -1
- package/dist/schemas/union.cjs +1 -1
- package/dist/schemas/union.cjs.map +1 -1
- package/dist/schemas/union.d.ts +9 -7
- package/dist/schemas/union.d.ts.map +1 -1
- package/dist/schemas/union.js +1 -1
- package/dist/schemas/union.js.map +1 -1
- package/dist/spell.cjs +2 -2
- package/dist/spell.cjs.map +1 -1
- package/dist/spell.iife.js +2 -2
- package/dist/spell.iife.js.map +1 -1
- package/dist/spell.js +842 -1601
- package/dist/spell.js.map +1 -1
- package/dist/types.cjs +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +151 -265
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/dist/core.cjs +0 -2
- package/dist/core.cjs.map +0 -1
- package/dist/core.d.ts +0 -101
- package/dist/core.d.ts.map +0 -1
- package/dist/core.js +0 -2
- package/dist/core.js.map +0 -1
- package/dist/formats.cjs +0 -2
- package/dist/formats.cjs.map +0 -1
- package/dist/formats.d.ts +0 -41
- package/dist/formats.d.ts.map +0 -1
- package/dist/formats.js +0 -2
- package/dist/formats.js.map +0 -1
- package/dist/json-schema.cjs +0 -2
- package/dist/json-schema.cjs.map +0 -1
- package/dist/json-schema.d.ts +0 -5
- package/dist/json-schema.d.ts.map +0 -1
- package/dist/json-schema.js +0 -2
- package/dist/json-schema.js.map +0 -1
- package/dist/safe-object.cjs +0 -2
- package/dist/safe-object.cjs.map +0 -1
- package/dist/safe-object.d.ts +0 -6
- package/dist/safe-object.d.ts.map +0 -1
- package/dist/safe-object.js +0 -2
- package/dist/safe-object.js.map +0 -1
- package/dist/schemas/boolean.cjs +0 -2
- package/dist/schemas/boolean.cjs.map +0 -1
- package/dist/schemas/boolean.d.ts +0 -9
- package/dist/schemas/boolean.d.ts.map +0 -1
- package/dist/schemas/boolean.js +0 -2
- package/dist/schemas/boolean.js.map +0 -1
- package/dist/schemas/enum.cjs +0 -2
- package/dist/schemas/enum.cjs.map +0 -1
- package/dist/schemas/enum.d.ts +0 -12
- package/dist/schemas/enum.d.ts.map +0 -1
- package/dist/schemas/enum.js +0 -2
- package/dist/schemas/enum.js.map +0 -1
- package/dist/schemas/instanceof.cjs +0 -2
- package/dist/schemas/instanceof.cjs.map +0 -1
- package/dist/schemas/instanceof.d.ts +0 -9
- package/dist/schemas/instanceof.d.ts.map +0 -1
- package/dist/schemas/instanceof.js +0 -2
- package/dist/schemas/instanceof.js.map +0 -1
- package/dist/schemas/literal.cjs +0 -2
- package/dist/schemas/literal.cjs.map +0 -1
- package/dist/schemas/literal.d.ts +0 -9
- package/dist/schemas/literal.d.ts.map +0 -1
- package/dist/schemas/literal.js +0 -2
- package/dist/schemas/literal.js.map +0 -1
- package/dist/schemas/never.cjs +0 -2
- package/dist/schemas/never.cjs.map +0 -1
- package/dist/schemas/never.d.ts +0 -8
- package/dist/schemas/never.d.ts.map +0 -1
- package/dist/schemas/never.js +0 -2
- package/dist/schemas/never.js.map +0 -1
- package/dist/schemas/raw-or.cjs +0 -2
- package/dist/schemas/raw-or.cjs.map +0 -1
- package/dist/schemas/raw-or.d.ts +0 -17
- package/dist/schemas/raw-or.d.ts.map +0 -1
- package/dist/schemas/raw-or.js +0 -2
- package/dist/schemas/raw-or.js.map +0 -1
- package/dist/schemas/variant.cjs +0 -2
- package/dist/schemas/variant.cjs.map +0 -1
- package/dist/schemas/variant.d.ts +0 -37
- package/dist/schemas/variant.d.ts.map +0 -1
- package/dist/schemas/variant.js +0 -2
- package/dist/schemas/variant.js.map +0 -1
- package/dist/tolerate.cjs +0 -2
- package/dist/tolerate.cjs.map +0 -1
- package/dist/tolerate.d.ts +0 -10
- package/dist/tolerate.d.ts.map +0 -1
- package/dist/tolerate.js +0 -2
- package/dist/tolerate.js.map +0 -1
- package/dist/validators.cjs +0 -2
- package/dist/validators.cjs.map +0 -1
- package/dist/validators.d.ts +0 -20
- package/dist/validators.d.ts.map +0 -1
- package/dist/validators.js +0 -2
- package/dist/validators.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.cjs","names":["Schema","fail","ErrorCode","resolveMessage","isEmail","isUrl","isUuid","isIsoDate","isIsoDateTime","isIp","isCuid","isCuid2","isUlid","isNanoid","isBase64","isBase64url","isHex","isHexColor","isEmoji","isJwt","isTime","isDuration","isSemver","isSlug","isNumeric","require_errors","require_types","require_core","require_formats","StringSchema","value","ctx","length","message","ann","current","exact","prefix","suffix","substr","safePattern","pattern","options","protocols","v"],"sources":["../../src/schemas/string.ts"],"sourcesContent":["import { warn } from '../_dev';\nimport type { MessageFn, SchemaDescriptor } from '../core';\nimport { ErrorCode, fail, resolveMessage, Schema } from '../core';\nimport {\n isBase64,\n isBase64url,\n isCuid,\n isCuid2,\n isDuration,\n isEmail,\n isEmoji,\n isHex,\n isHexColor,\n isIp,\n isIsoDate,\n isIsoDateTime,\n isJwt,\n isNanoid,\n isNumeric,\n isSemver,\n isSlug,\n isTime,\n isUlid,\n isUrl,\n isUuid,\n} from '../formats';\n\n/* -------------------- Typed annotations -------------------- */\n\ninterface StringAnnotations extends Record<string, unknown> {\n contentEncoding?: string;\n format?: string;\n maxLength?: number;\n minLength?: number;\n pattern?: string | null;\n}\n\ntype UrlOptions = {\n message?: MessageFn<{ value: string }>;\n protocols?: readonly string[];\n};\n\nexport class StringSchema<Input = string> extends Schema<string, Input> {\n protected override get _kind(): string {\n return 'string';\n }\n\n constructor() {\n super((value, ctx) =>\n typeof value === 'string' ? null : fail(ErrorCode.invalid_type, ctx!.messages.string.type()),\n );\n }\n\n min(length: number, message?: MessageFn<{ min: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length >= length) return null;\n\n return fail(\n ErrorCode.too_small,\n resolveMessage(message ?? ctx!.messages.string.min, { min: length, value: value as string }),\n {\n min: length,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return {\n ...ann,\n minLength: ann.minLength === undefined ? length : Math.max(ann.minLength, length),\n };\n },\n );\n }\n\n max(length: number, message?: MessageFn<{ max: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length <= length) return null;\n\n return fail(\n ErrorCode.too_big,\n resolveMessage(message ?? ctx!.messages.string.max, { max: length, value: value as string }),\n {\n max: length,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return {\n ...ann,\n maxLength: ann.maxLength === undefined ? length : Math.min(ann.maxLength, length),\n };\n },\n );\n }\n\n length(exact: number, message?: MessageFn<{ exact: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length === exact) return null;\n\n return fail(\n ErrorCode.invalid_length,\n resolveMessage(message ?? ctx!.messages.string.length, { exact, value: value as string }),\n { exact },\n );\n },\n (ann) => ({ ...ann, maxLength: exact, minLength: exact }),\n );\n }\n\n nonEmpty(message?: MessageFn<{ min: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length > 0) return null;\n\n return fail(\n ErrorCode.too_small,\n resolveMessage(message ?? ctx!.messages.string.nonEmpty, { min: 1, value: value as string }),\n { min: 1 },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return { ...ann, minLength: Math.max(ann.minLength ?? 0, 1) };\n },\n );\n }\n\n startsWith(prefix: string, message?: MessageFn<{ prefix: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).startsWith(prefix)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.startsWith, { prefix, value: value as string }),\n { prefix },\n );\n });\n }\n\n endsWith(suffix: string, message?: MessageFn<{ suffix: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).endsWith(suffix)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.endsWith, { suffix, value: value as string }),\n { suffix },\n );\n });\n }\n\n includes(substr: string, message?: MessageFn<{ substr: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).includes(substr)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.includes, { substr, value: value as string }),\n {\n includes: substr,\n },\n );\n });\n }\n\n /**\n * Validates that the string matches the given regular expression.\n *\n * **Security note:** Stateful `/g` and `/y` flags are stripped automatically to prevent\n * `lastIndex`-based bugs. However, caller-supplied patterns with catastrophic backtracking\n * (e.g. `/(a+)+$/`) are a ReDoS risk when validating untrusted input in server-side contexts.\n * Prefer well-tested, bounded patterns for user-facing validation.\n *\n */\n regex(pattern: RegExp, message?: MessageFn<{ value: string }>): this {\n const safePattern = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ''));\n\n return this._addConstraint(\n (value, ctx) => {\n // Caller-supplied regexes still run against untrusted strings; spell can\n // neutralize stateful /g and /y flags but cannot make arbitrary patterns\n // immune to catastrophic backtracking without breaking the API.\n if (safePattern.test(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.regex, { value: value as string }),\n {\n pattern: safePattern.source,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n if (ann.pattern === null) return ann; // already ambiguous\n\n if (ann.pattern === undefined) return { ...ann, pattern: safePattern.source };\n\n if (ann.pattern !== safePattern.source) {\n warn(\n '[spell] Multiple .regex() constraints detected on a single string schema. ' +\n 'JSON Schema `pattern` cannot represent multiple patterns and will be omitted from definition output.',\n );\n\n return { ...ann, pattern: null };\n }\n\n return ann;\n },\n );\n }\n\n email(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isEmail(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.email, { value: value as string }),\n { format: 'email' },\n );\n },\n (ann) => ({ ...ann, format: 'email' }),\n );\n }\n\n url(options: UrlOptions = {}): this {\n const { message, protocols = ['http', 'https'] } = options;\n\n return this._addConstraint(\n (value, ctx) => {\n if (isUrl(value as string, protocols)) return null;\n\n return fail(\n ErrorCode.invalid_url,\n resolveMessage(message ?? ctx!.messages.string.url, { value: value as string }),\n { format: 'url' },\n );\n },\n (ann) => ({ ...ann, format: 'uri' }),\n );\n }\n\n uuid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isUuid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.uuid, { value: value as string }),\n { format: 'uuid' },\n );\n },\n (ann) => ({ ...ann, format: 'uuid' }),\n );\n }\n\n isoDate(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isIsoDate(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.date, { value: value as string }),\n {\n format: 'iso-date',\n },\n );\n },\n (ann) => ({ ...ann, format: 'date' }),\n );\n }\n\n isoDateTime(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isIsoDateTime(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.dateTime, { value: value as string }),\n {\n format: 'iso-datetime',\n },\n );\n },\n (ann) => ({ ...ann, format: 'date-time' }),\n );\n }\n\n ip(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isIp(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.ip, { value: value as string }),\n { format: 'ip' },\n );\n });\n }\n\n cuid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isCuid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.cuid, { value: value as string }),\n { format: 'cuid' },\n );\n });\n }\n\n cuid2(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isCuid2(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.cuid2, { value: value as string }),\n { format: 'cuid2' },\n );\n });\n }\n\n ulid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isUlid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.ulid, { value: value as string }),\n { format: 'ulid' },\n );\n });\n }\n\n nanoid(length?: number, message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isNanoid(value as string, length)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.nanoid, { value: value as string }),\n { format: 'nanoid' },\n );\n });\n }\n\n base64(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isBase64(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_base64,\n resolveMessage(message ?? ctx!.messages.string.base64, { value: value as string }),\n {\n format: 'base64',\n },\n );\n },\n (ann) => ({ ...ann, contentEncoding: 'base64' }),\n );\n }\n\n base64url(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isBase64url(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_base64,\n resolveMessage(message ?? ctx!.messages.string.base64url, { value: value as string }),\n {\n format: 'base64url',\n },\n );\n });\n }\n\n hex(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isHex(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.hex, { value: value as string }),\n { format: 'hex' },\n );\n });\n }\n\n hexColor(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isHexColor(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.hexColor, { value: value as string }),\n {\n format: 'hex-color',\n },\n );\n });\n }\n\n emoji(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isEmoji(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.emoji, { value: value as string }),\n { format: 'emoji' },\n );\n });\n }\n\n jwt(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isJwt(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.jwt, { value: value as string }),\n { format: 'jwt' },\n );\n });\n }\n\n time(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isTime(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.time, { value: value as string }),\n { format: 'time' },\n );\n });\n }\n\n duration(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isDuration(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_duration,\n resolveMessage(message ?? ctx!.messages.string.duration, { value: value as string }),\n {\n format: 'duration',\n },\n );\n },\n (ann) => ({ ...ann, format: 'duration' }),\n );\n }\n\n semver(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isSemver(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.semver, { value: value as string }),\n { format: 'semver' },\n );\n });\n }\n\n slug(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isSlug(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.slug, { value: value as string }),\n { format: 'slug' },\n );\n });\n }\n\n numeric(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isNumeric(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.numeric, { value: value as string }),\n { format: 'numeric' },\n );\n });\n }\n\n /**\n * **Note:** `trim()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n trim(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.trim() : v));\n }\n\n /**\n * **Note:** `lowercase()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n lowercase(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.toLowerCase() : v));\n }\n\n /**\n * **Note:** `uppercase()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n uppercase(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.toUpperCase() : v));\n }\n\n protected override _toDescriptorImpl(): SchemaDescriptor {\n const ann = this._annotations as StringAnnotations;\n\n return {\n ...this._describeBase(),\n ...(ann.contentEncoding !== undefined ? { contentEncoding: ann.contentEncoding } : {}),\n ...(ann.format !== undefined ? { format: ann.format } : {}),\n ...(ann.maxLength !== undefined ? { maxLength: ann.maxLength } : {}),\n ...(ann.minLength !== undefined ? { minLength: ann.minLength } : {}),\n ...(ann.pattern !== undefined ? { pattern: ann.pattern } : {}),\n kind: 'string',\n };\n }\n\n static coerce(): StringSchema<unknown> {\n return new StringSchema().preprocess((v: unknown) => (v == null ? v : String(v)));\n }\n}\n"],"mappings":"A,6IA0CA,IAAa6B,EAAb,MAAaA,UAAqC7B,EAAAA,MAAsB,CACtE,IAAuB,OAAgB,CACrC,MAAO,QACT,CAEA,aAAc,CACZ,OAAO,EAAO,IACZ,OAAO8B,GAAU,SAAW,KAAO7B,EAAAA,KAAKC,EAAAA,UAAU,aAAc6B,EAAK,SAAS,OAAO,KAAK,CAAC,CAC7F,CACF,CAEA,IAAI,EAAgB,EAA2D,CAC7E,OAAO,KAAK,gBACT,EAAO,IACDD,EAAiB,QAAUE,EAAe,KAExC/B,EAAAA,KACLC,EAAAA,UAAU,UACVC,EAAAA,eAAe8B,GAAWF,EAAK,SAAS,OAAO,IAAK,CAAE,IAAKC,EAAe,OAAgB,CAAC,EAC3F,CACE,IAAKA,CACP,CACF,EAED,GAAY,CACX,IAAME,EAAMC,EAEZ,MAAO,CACL,GAAGD,EACH,UAAWA,EAAI,YAAc,IAAA,GAAYF,EAAS,KAAK,IAAIE,EAAI,UAAWF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","ok","invalid","issue","ErrorCode","resolveMessage","atLeast","atMost","require__parse","require_predicates","require_schema","require_types","escapeRegExp","text","StringSchema","value","ctx","test","message","m","format","meta","current","fail","pattern","length","exact","prefix","suffix","substr","safe","options","protocols"],"sources":["../../src/schemas/string.ts"],"sourcesContent":["import {\n atLeast,\n atMost,\n invalid,\n issue,\n type MaybePromise,\n ok,\n type ParseValue,\n type RunContext,\n resolveMessage,\n} from '../_parse';\nimport type { Messages } from '../messages';\nimport * as is from '../predicates';\nimport { Schema } from '../schema';\nimport {\n ErrorCode,\n type Issue,\n type MessageFn,\n type PatternDescriptor,\n type SchemaDescriptor,\n type SchemaKind,\n} from '../types';\n\ntype StringMeta = {\n formats: readonly string[];\n maxLength: number;\n minLength: number;\n patterns: readonly PatternDescriptor[];\n};\n\ntype ValueMessage = MessageFn<{ value: string }>;\n\n/** Each format is a `StringSchema` method, a `Messages['string']` key, and the issue's `params.format`. */\ntype StringFormat =\n | 'base64'\n | 'base64url'\n | 'cuid'\n | 'cuid2'\n | 'duration'\n | 'email'\n | 'emoji'\n | 'hex'\n | 'hexColor'\n | 'ip'\n | 'isoDate'\n | 'isoDateTime'\n | 'jwt'\n | 'nanoid'\n | 'numeric'\n | 'semver'\n | 'slug'\n | 'time'\n | 'ulid'\n | 'url'\n | 'uuid';\n\nconst escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport class StringSchema<Input = string> extends Schema<string, Input> {\n override get kind(): SchemaKind {\n return 'string';\n }\n\n protected override _parse(value: unknown, ctx: RunContext): MaybePromise<ParseValue> {\n return typeof value === 'string'\n ? ok(value)\n : invalid(value, issue(ErrorCode.invalid_type, ctx.messages.string.type()));\n }\n\n private _format(\n format: StringFormat,\n test: (value: string) => boolean,\n message: ValueMessage | undefined,\n meta?: (current: Partial<StringMeta>) => Partial<StringMeta>,\n ): this {\n return this._refine<StringMeta>(\n test,\n (value, m) => issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string[format], { value }), { format }),\n (current) => ({ ...meta?.(current), formats: [...(current.formats ?? []), format] }),\n );\n }\n\n private _pattern(\n test: (value: string) => boolean,\n pattern: PatternDescriptor,\n fail: (value: string, messages: Messages) => Issue,\n ): this {\n return this._refine<StringMeta>(test, fail, (current) => ({ patterns: [...(current.patterns ?? []), pattern] }));\n }\n\n /* -------------------- Length -------------------- */\n\n min(length: number, message?: MessageFn<{ min: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length >= length,\n (value, m) =>\n issue(ErrorCode.too_small, resolveMessage(message ?? m.string.min, { min: length, value }), { min: length }),\n (current) => ({ minLength: atLeast(current.minLength, length) }),\n );\n }\n\n max(length: number, message?: MessageFn<{ max: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length <= length,\n (value, m) =>\n issue(ErrorCode.too_big, resolveMessage(message ?? m.string.max, { max: length, value }), { max: length }),\n (current) => ({ maxLength: atMost(current.maxLength, length) }),\n );\n }\n\n length(exact: number, message?: MessageFn<{ exact: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length === exact,\n (value, m) =>\n issue(ErrorCode.invalid_length, resolveMessage(message ?? m.string.length, { exact, value }), { exact }),\n (current) => ({ maxLength: atMost(current.maxLength, exact), minLength: atLeast(current.minLength, exact) }),\n );\n }\n\n nonEmpty(message?: ValueMessage): this {\n return this._refine<StringMeta>(\n (value) => value.length > 0,\n (value, m) => issue(ErrorCode.too_small, resolveMessage(message ?? m.string.nonEmpty, { value }), { min: 1 }),\n (current) => ({ minLength: atLeast(current.minLength, 1) }),\n );\n }\n\n /* -------------------- Patterns -------------------- */\n\n startsWith(prefix: string, message?: MessageFn<{ prefix: string; value: string }>): this {\n return this._pattern(\n (value) => value.startsWith(prefix),\n { flags: '', source: `^${escapeRegExp(prefix)}` },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.startsWith, { prefix, value }), {\n format: 'startsWith',\n prefix,\n }),\n );\n }\n\n endsWith(suffix: string, message?: MessageFn<{ suffix: string; value: string }>): this {\n return this._pattern(\n (value) => value.endsWith(suffix),\n { flags: '', source: `${escapeRegExp(suffix)}$` },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.endsWith, { suffix, value }), {\n format: 'endsWith',\n suffix,\n }),\n );\n }\n\n includes(substr: string, message?: MessageFn<{ substr: string; value: string }>): this {\n return this._pattern(\n (value) => value.includes(substr),\n { flags: '', source: escapeRegExp(substr) },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.includes, { substr, value }), {\n format: 'includes',\n includes: substr,\n }),\n );\n }\n\n /**\n * Validates against `pattern`. Stateful `g`/`y` flags are dropped so repeated parses agree.\n *\n * **Security:** caller-supplied patterns with catastrophic backtracking (e.g. `/(a+)+$/`) are a\n * ReDoS risk on untrusted input; prefer bounded patterns.\n */\n regex(pattern: RegExp, message?: ValueMessage): this {\n const safe = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ''));\n\n return this._pattern(\n (value) => safe.test(value),\n { flags: safe.flags, source: safe.source },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.regex, { value }), {\n format: 'regex',\n pattern: safe.source,\n }),\n );\n }\n\n /* -------------------- Formats -------------------- */\n\n email(message?: ValueMessage): this {\n return this._format('email', is.isEmail, message);\n }\n\n /** Absolute URL whose protocol is in `protocols` (default `http`, `https`). */\n url(options: { message?: ValueMessage; protocols?: readonly string[] } = {}): this {\n const { message, protocols = ['http', 'https'] } = options;\n const pattern = { flags: 'i', source: `^(?:${protocols.map(escapeRegExp).join('|')}):` };\n\n return this._format(\n 'url',\n (value) => is.isUrl(value, protocols),\n message,\n (current) => ({\n patterns: [...(current.patterns ?? []), pattern],\n }),\n );\n }\n\n uuid(message?: ValueMessage): this {\n return this._format('uuid', is.isUuid, message);\n }\n\n isoDate(message?: ValueMessage): this {\n return this._format('isoDate', is.isIsoDate, message);\n }\n\n isoDateTime(message?: ValueMessage): this {\n return this._format('isoDateTime', is.isIsoDateTime, message);\n }\n\n time(message?: ValueMessage): this {\n return this._format('time', is.isTime, message);\n }\n\n duration(message?: ValueMessage): this {\n return this._format('duration', is.isDuration, message);\n }\n\n ip(message?: ValueMessage): this {\n return this._format('ip', is.isIp, message);\n }\n\n cuid(message?: ValueMessage): this {\n return this._format('cuid', is.isCuid, message);\n }\n\n cuid2(message?: ValueMessage): this {\n return this._format('cuid2', is.isCuid2, message);\n }\n\n ulid(message?: ValueMessage): this {\n return this._format('ulid', is.isUlid, message);\n }\n\n /** NanoID of exactly `length` characters (default 21). */\n nanoid(length = 21, message?: ValueMessage): this {\n return this._format(\n 'nanoid',\n (value) => is.isNanoid(value, length),\n message,\n (current) => ({\n maxLength: atMost(current.maxLength, length),\n minLength: atLeast(current.minLength, length),\n }),\n );\n }\n\n base64(message?: ValueMessage): this {\n return this._format('base64', is.isBase64, message);\n }\n\n base64url(message?: ValueMessage): this {\n return this._format('base64url', is.isBase64url, message);\n }\n\n hex(message?: ValueMessage): this {\n return this._format('hex', is.isHex, message);\n }\n\n hexColor(message?: ValueMessage): this {\n return this._format('hexColor', is.isHexColor, message);\n }\n\n emoji(message?: ValueMessage): this {\n return this._format('emoji', is.isEmoji, message);\n }\n\n jwt(message?: ValueMessage): this {\n return this._format('jwt', is.isJwt, message);\n }\n\n semver(message?: ValueMessage): this {\n return this._format('semver', is.isSemver, message);\n }\n\n slug(message?: ValueMessage): this {\n return this._format('slug', is.isSlug, message);\n }\n\n numeric(message?: ValueMessage): this {\n return this._format('numeric', is.isNumeric, message);\n }\n\n /* -------------------- Normalization (ordered transforms) -------------------- */\n // Constraints chained after a normalizer see the normalized value. Normalizers are\n // transforms, so `definition()` throws for schemas that use them.\n\n trim(): this {\n return this.transform((value) => value.trim()) as unknown as this;\n }\n\n lowercase(): this {\n return this.transform((value) => value.toLowerCase()) as unknown as this;\n }\n\n uppercase(): this {\n return this.transform((value) => value.toUpperCase()) as unknown as this;\n }\n\n protected override _describe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CAChC,OAAO,KAAK,QAAQ,MAAAT,EAAA,MAAiBS,CAAO,CAC9C,CAEA,SAAS,EAA8B,CACrC,OAAO,KAAK,QAAQ,WAAAT,EAAA,WAA2BS,CAAO,CACxD,CAEA,MAAM,EAA8B,CAClC,OAAO,KAAK,QAAQ,QAAAT,EAAA,QAAqBS,CAAO,CAClD,CAEA,IAAI,EAA8B,CAChC,OAAO,KAAK,QAAQ,MAAAT,EAAA,MAAiBS,CAAO,CAC9C,CAEA,OAAO,EAA8B,CACnC,OAAO,KAAK,QAAQ,SAAAT,EAAA,SAAuBS,CAAO,CACpD,CAEA,KAAK,EAA8B,CACjC,OAAO,KAAK,QAAQ,OAAAT,EAAA,OAAmBS,CAAO,CAChD,CAEA,QAAQ,EAA8B,CACpC,OAAO,KAAK,QAAQ,UAAAT,EAAA,UAAyBS,CAAO,CACtD,CAMA,MAAa,CACX,OAAO,KAAK,UAAW,GAAUH,EAAM,KAAK,CAAC,CAC/C,CAEA,WAAkB,CAChB,OAAO,KAAK,UAAW,GAAUA,EAAM,YAAY,CAAC,CACtD,CAEA,WAAkB,CAChB,OAAO,KAAK,UAAW,GAAUA,EAAM,YAAY,CAAC,CACtD,CAEA,WAAiD,CAC/C,MAAO,CAAE,GAAG,KAAK,cAAc,EAAG,GAAI,KAAK,MAAM,KAA8B,KAAM,QAAS,CAChG,CACF,E"}
|
package/dist/schemas/string.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { Schema } from '../
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
protocols?: readonly string[];
|
|
8
|
-
};
|
|
1
|
+
import { type MaybePromise, type ParseValue, type RunContext } from '../_parse';
|
|
2
|
+
import { Schema } from '../schema';
|
|
3
|
+
import { type MessageFn, type SchemaDescriptor, type SchemaKind } from '../types';
|
|
4
|
+
type ValueMessage = MessageFn<{
|
|
5
|
+
value: string;
|
|
6
|
+
}>;
|
|
9
7
|
export declare class StringSchema<Input = string> extends Schema<string, Input> {
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
get kind(): SchemaKind;
|
|
9
|
+
protected _parse(value: unknown, ctx: RunContext): MaybePromise<ParseValue>;
|
|
10
|
+
private _format;
|
|
11
|
+
private _pattern;
|
|
12
12
|
min(length: number, message?: MessageFn<{
|
|
13
13
|
min: number;
|
|
14
14
|
value: string;
|
|
@@ -21,10 +21,7 @@ export declare class StringSchema<Input = string> extends Schema<string, Input>
|
|
|
21
21
|
exact: number;
|
|
22
22
|
value: string;
|
|
23
23
|
}>): this;
|
|
24
|
-
nonEmpty(message?:
|
|
25
|
-
min: number;
|
|
26
|
-
value: string;
|
|
27
|
-
}>): this;
|
|
24
|
+
nonEmpty(message?: ValueMessage): this;
|
|
28
25
|
startsWith(prefix: string, message?: MessageFn<{
|
|
29
26
|
prefix: string;
|
|
30
27
|
value: string;
|
|
@@ -38,95 +35,42 @@ export declare class StringSchema<Input = string> extends Schema<string, Input>
|
|
|
38
35
|
value: string;
|
|
39
36
|
}>): this;
|
|
40
37
|
/**
|
|
41
|
-
* Validates
|
|
42
|
-
*
|
|
43
|
-
* **Security note:** Stateful `/g` and `/y` flags are stripped automatically to prevent
|
|
44
|
-
* `lastIndex`-based bugs. However, caller-supplied patterns with catastrophic backtracking
|
|
45
|
-
* (e.g. `/(a+)+$/`) are a ReDoS risk when validating untrusted input in server-side contexts.
|
|
46
|
-
* Prefer well-tested, bounded patterns for user-facing validation.
|
|
38
|
+
* Validates against `pattern`. Stateful `g`/`y` flags are dropped so repeated parses agree.
|
|
47
39
|
*
|
|
40
|
+
* **Security:** caller-supplied patterns with catastrophic backtracking (e.g. `/(a+)+$/`) are a
|
|
41
|
+
* ReDoS risk on untrusted input; prefer bounded patterns.
|
|
48
42
|
*/
|
|
49
|
-
regex(pattern: RegExp, message?:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
uuid(message?:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}>): this;
|
|
77
|
-
nanoid(length?: number, message?: MessageFn<{
|
|
78
|
-
value: string;
|
|
79
|
-
}>): this;
|
|
80
|
-
base64(message?: MessageFn<{
|
|
81
|
-
value: string;
|
|
82
|
-
}>): this;
|
|
83
|
-
base64url(message?: MessageFn<{
|
|
84
|
-
value: string;
|
|
85
|
-
}>): this;
|
|
86
|
-
hex(message?: MessageFn<{
|
|
87
|
-
value: string;
|
|
88
|
-
}>): this;
|
|
89
|
-
hexColor(message?: MessageFn<{
|
|
90
|
-
value: string;
|
|
91
|
-
}>): this;
|
|
92
|
-
emoji(message?: MessageFn<{
|
|
93
|
-
value: string;
|
|
94
|
-
}>): this;
|
|
95
|
-
jwt(message?: MessageFn<{
|
|
96
|
-
value: string;
|
|
97
|
-
}>): this;
|
|
98
|
-
time(message?: MessageFn<{
|
|
99
|
-
value: string;
|
|
100
|
-
}>): this;
|
|
101
|
-
duration(message?: MessageFn<{
|
|
102
|
-
value: string;
|
|
103
|
-
}>): this;
|
|
104
|
-
semver(message?: MessageFn<{
|
|
105
|
-
value: string;
|
|
106
|
-
}>): this;
|
|
107
|
-
slug(message?: MessageFn<{
|
|
108
|
-
value: string;
|
|
109
|
-
}>): this;
|
|
110
|
-
numeric(message?: MessageFn<{
|
|
111
|
-
value: string;
|
|
112
|
-
}>): this;
|
|
113
|
-
/**
|
|
114
|
-
* **Note:** `trim()` adds a preprocessor. Preprocessors are not serializable :
|
|
115
|
-
* `definition()` throws because preprocessors are not serializable.
|
|
116
|
-
*/
|
|
43
|
+
regex(pattern: RegExp, message?: ValueMessage): this;
|
|
44
|
+
email(message?: ValueMessage): this;
|
|
45
|
+
/** Absolute URL whose protocol is in `protocols` (default `http`, `https`). */
|
|
46
|
+
url(options?: {
|
|
47
|
+
message?: ValueMessage;
|
|
48
|
+
protocols?: readonly string[];
|
|
49
|
+
}): this;
|
|
50
|
+
uuid(message?: ValueMessage): this;
|
|
51
|
+
isoDate(message?: ValueMessage): this;
|
|
52
|
+
isoDateTime(message?: ValueMessage): this;
|
|
53
|
+
time(message?: ValueMessage): this;
|
|
54
|
+
duration(message?: ValueMessage): this;
|
|
55
|
+
ip(message?: ValueMessage): this;
|
|
56
|
+
cuid(message?: ValueMessage): this;
|
|
57
|
+
cuid2(message?: ValueMessage): this;
|
|
58
|
+
ulid(message?: ValueMessage): this;
|
|
59
|
+
/** NanoID of exactly `length` characters (default 21). */
|
|
60
|
+
nanoid(length?: number, message?: ValueMessage): this;
|
|
61
|
+
base64(message?: ValueMessage): this;
|
|
62
|
+
base64url(message?: ValueMessage): this;
|
|
63
|
+
hex(message?: ValueMessage): this;
|
|
64
|
+
hexColor(message?: ValueMessage): this;
|
|
65
|
+
emoji(message?: ValueMessage): this;
|
|
66
|
+
jwt(message?: ValueMessage): this;
|
|
67
|
+
semver(message?: ValueMessage): this;
|
|
68
|
+
slug(message?: ValueMessage): this;
|
|
69
|
+
numeric(message?: ValueMessage): this;
|
|
117
70
|
trim(): this;
|
|
118
|
-
/**
|
|
119
|
-
* **Note:** `lowercase()` adds a preprocessor. Preprocessors are not serializable :
|
|
120
|
-
* `definition()` throws because preprocessors are not serializable.
|
|
121
|
-
*/
|
|
122
71
|
lowercase(): this;
|
|
123
|
-
/**
|
|
124
|
-
* **Note:** `uppercase()` adds a preprocessor. Preprocessors are not serializable :
|
|
125
|
-
* `definition()` throws because preprocessors are not serializable.
|
|
126
|
-
*/
|
|
127
72
|
uppercase(): this;
|
|
128
|
-
protected
|
|
129
|
-
static coerce(): StringSchema<unknown>;
|
|
73
|
+
protected _describe(): SchemaDescriptor;
|
|
130
74
|
}
|
|
131
75
|
export {};
|
|
132
76
|
//# sourceMappingURL=string.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schemas/string.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schemas/string.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,YAAY,EAEjB,KAAK,UAAU,EACf,KAAK,UAAU,EAEhB,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAGL,KAAK,SAAS,EAEd,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,UAAU,CAAC;AASlB,KAAK,YAAY,GAAG,SAAS,CAAC;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AA4BjD,qBAAa,YAAY,CAAC,KAAK,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IACrE,IAAa,IAAI,IAAI,UAAU,CAE9B;cAEkB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;IAMpF,OAAO,CAAC,OAAO;IAaf,OAAO,CAAC,QAAQ;IAUhB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG,IAAI;IAS9E,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAAG,IAAI;IAS9E,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC;QAAE,KAAK,E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IAIhC,IAAI,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIlC,KAAK,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAInC,IAAI,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIlC,0DAA0D;IAC1D,MAAM,CAAC,MAAM,SAAK,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAYjD,MAAM,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIpC,SAAS,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIvC,GAAG,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIjC,QAAQ,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAItC,KAAK,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAInC,GAAG,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIjC,MAAM,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIpC,IAAI,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAIlC,OAAO,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAQrC,IAAI,IAAI,IAAI;IAIZ,SAAS,IAAI,IAAI;IAIjB,SAAS,IAAI,IAAI;cAIE,SAAS,IAAI,gBAAgB;CAGjD"}
|
package/dist/schemas/string.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{atLeast as e,atMost as t,invalid as n,issue as r,ok as i,resolveMessage as a}from"../_parse.js";import{isBase64 as o,isBase64url as s,isCuid as c,isCuid2 as l,isDuration as u,isEmail as d,isEmoji as f,isHex as p,isHexColor as m,isIp as h,isIsoDate as g,isIsoDateTime as _,isJwt as v,isNanoid as y,isNumeric as b,isSemver as x,isSlug as S,isTime as C,isUlid as w,isUrl as T,isUuid as E}from"../predicates.js";import{Schema as D}from"../schema.js";import{ErrorCode as O}from"../types.js";var k=e=>e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`),A=class extends D{get kind(){return`string`}_parse(e,t){return typeof e==`string`?i(e):n(e,r(O.invalid_type,t.messages.string.type()))}_format(e,t,n,i){return this._refine(t,(t,i)=>r(O.invalid_format,a(n??i.string[e],{value:t}),{format:e}),t=>({...i?.(t),formats:[...t.formats??[],e]}))}_pattern(e,t,n){return this._refine(e,n,e=>({patterns:[...e.patterns??[],t]}))}min(t,n){return this._refine(e=>e.length>=t,(e,i)=>r(O.too_small,a(n??i.string.min,{min:t,value:e}),{min:t}),n=>({minLength:e(n.minLength,t)}))}max(e,n){return this._refine(t=>t.length<=e,(t,i)=>r(O.too_big,a(n??i.string.max,{max:e,value:t}),{max:e}),n=>({maxLength:t(n.maxLength,e)}))}length(n,i){return this._refine(e=>e.length===n,(e,t)=>r(O.invalid_length,a(i??t.string.length,{exact:n,value:e}),{exact:n}),r=>({maxLength:t(r.maxLength,n),minLength:e(r.minLength,n)}))}nonEmpty(t){return this._refine(e=>e.length>0,(e,n)=>r(O.too_small,a(t??n.string.nonEmpty,{value:e}),{min:1}),t=>({minLength:e(t.minLength,1)}))}startsWith(e,t){return this._pattern(t=>t.startsWith(e),{flags:``,source:`^${k(e)}`},(n,i)=>r(O.invalid_format,a(t??i.string.startsWith,{prefix:e,value:n}),{format:`startsWith`,prefix:e}))}endsWith(e,t){return this._pattern(t=>t.endsWith(e),{flags:``,source:`${k(e)}$`},(n,i)=>r(O.invalid_format,a(t??i.string.endsWith,{suffix:e,value:n}),{format:`endsWith`,suffix:e}))}includes(e,t){return this._pattern(t=>t.includes(e),{flags:``,source:k(e)},(n,i)=>r(O.invalid_format,a(t??i.string.includes,{substr:e,value:n}),{format:`includes`,includes:e}))}regex(e,t){let n=new RegExp(e.source,e.flags.replace(/[gy]/g,``));return this._pattern(e=>n.test(e),{flags:n.flags,source:n.source},(e,i)=>r(O.invalid_format,a(t??i.string.regex,{value:e}),{format:`regex`,pattern:n.source}))}email(e){return this._format(`email`,d,e)}url(e={}){let{message:t,protocols:n=[`http`,`https`]}=e,r={flags:`i`,source:`^(?:${n.map(k).join(`|`)}):`};return this._format(`url`,e=>T(e,n),t,e=>({patterns:[...e.patterns??[],r]}))}uuid(e){return this._format(`uuid`,E,e)}isoDate(e){return this._format(`isoDate`,g,e)}isoDateTime(e){return this._format(`isoDateTime`,_,e)}time(e){return this._format(`time`,C,e)}duration(e){return this._format(`duration`,u,e)}ip(e){return this._format(`ip`,h,e)}cuid(e){return this._format(`cuid`,c,e)}cuid2(e){return this._format(`cuid2`,l,e)}ulid(e){return this._format(`ulid`,w,e)}nanoid(n=21,r){return this._format(`nanoid`,e=>y(e,n),r,r=>({maxLength:t(r.maxLength,n),minLength:e(r.minLength,n)}))}base64(e){return this._format(`base64`,o,e)}base64url(e){return this._format(`base64url`,s,e)}hex(e){return this._format(`hex`,p,e)}hexColor(e){return this._format(`hexColor`,m,e)}emoji(e){return this._format(`emoji`,f,e)}jwt(e){return this._format(`jwt`,v,e)}semver(e){return this._format(`semver`,x,e)}slug(e){return this._format(`slug`,S,e)}numeric(e){return this._format(`numeric`,b,e)}trim(){return this.transform(e=>e.trim())}lowercase(){return this.transform(e=>e.toLowerCase())}uppercase(){return this.transform(e=>e.toUpperCase())}_describe(){return{...this._describeBase(),...this.state.meta,kind:`string`}}};export{A as StringSchema};
|
|
2
2
|
//# sourceMappingURL=string.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","names":["StringSchema","Schema","value","fail","ErrorCode","ctx","length","resolveMessage","message","ann","current","exact","prefix","suffix","substr","safePattern","pattern","isEmail","options","isUrl","protocols","isUuid","isIsoDate","isIsoDateTime","isIp","isCuid","isCuid2","isUlid","isNanoid","isBase64","isBase64url","isHex","isHexColor","isEmoji","isJwt","isTime","isDuration","isSemver","isSlug","isNumeric","v"],"sources":["../../src/schemas/string.ts"],"sourcesContent":["import { warn } from '../_dev';\nimport type { MessageFn, SchemaDescriptor } from '../core';\nimport { ErrorCode, fail, resolveMessage, Schema } from '../core';\nimport {\n isBase64,\n isBase64url,\n isCuid,\n isCuid2,\n isDuration,\n isEmail,\n isEmoji,\n isHex,\n isHexColor,\n isIp,\n isIsoDate,\n isIsoDateTime,\n isJwt,\n isNanoid,\n isNumeric,\n isSemver,\n isSlug,\n isTime,\n isUlid,\n isUrl,\n isUuid,\n} from '../formats';\n\n/* -------------------- Typed annotations -------------------- */\n\ninterface StringAnnotations extends Record<string, unknown> {\n contentEncoding?: string;\n format?: string;\n maxLength?: number;\n minLength?: number;\n pattern?: string | null;\n}\n\ntype UrlOptions = {\n message?: MessageFn<{ value: string }>;\n protocols?: readonly string[];\n};\n\nexport class StringSchema<Input = string> extends Schema<string, Input> {\n protected override get _kind(): string {\n return 'string';\n }\n\n constructor() {\n super((value, ctx) =>\n typeof value === 'string' ? null : fail(ErrorCode.invalid_type, ctx!.messages.string.type()),\n );\n }\n\n min(length: number, message?: MessageFn<{ min: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length >= length) return null;\n\n return fail(\n ErrorCode.too_small,\n resolveMessage(message ?? ctx!.messages.string.min, { min: length, value: value as string }),\n {\n min: length,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return {\n ...ann,\n minLength: ann.minLength === undefined ? length : Math.max(ann.minLength, length),\n };\n },\n );\n }\n\n max(length: number, message?: MessageFn<{ max: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length <= length) return null;\n\n return fail(\n ErrorCode.too_big,\n resolveMessage(message ?? ctx!.messages.string.max, { max: length, value: value as string }),\n {\n max: length,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return {\n ...ann,\n maxLength: ann.maxLength === undefined ? length : Math.min(ann.maxLength, length),\n };\n },\n );\n }\n\n length(exact: number, message?: MessageFn<{ exact: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length === exact) return null;\n\n return fail(\n ErrorCode.invalid_length,\n resolveMessage(message ?? ctx!.messages.string.length, { exact, value: value as string }),\n { exact },\n );\n },\n (ann) => ({ ...ann, maxLength: exact, minLength: exact }),\n );\n }\n\n nonEmpty(message?: MessageFn<{ min: number; value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if ((value as string).length > 0) return null;\n\n return fail(\n ErrorCode.too_small,\n resolveMessage(message ?? ctx!.messages.string.nonEmpty, { min: 1, value: value as string }),\n { min: 1 },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n return { ...ann, minLength: Math.max(ann.minLength ?? 0, 1) };\n },\n );\n }\n\n startsWith(prefix: string, message?: MessageFn<{ prefix: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).startsWith(prefix)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.startsWith, { prefix, value: value as string }),\n { prefix },\n );\n });\n }\n\n endsWith(suffix: string, message?: MessageFn<{ suffix: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).endsWith(suffix)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.endsWith, { suffix, value: value as string }),\n { suffix },\n );\n });\n }\n\n includes(substr: string, message?: MessageFn<{ substr: string; value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if ((value as string).includes(substr)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.includes, { substr, value: value as string }),\n {\n includes: substr,\n },\n );\n });\n }\n\n /**\n * Validates that the string matches the given regular expression.\n *\n * **Security note:** Stateful `/g` and `/y` flags are stripped automatically to prevent\n * `lastIndex`-based bugs. However, caller-supplied patterns with catastrophic backtracking\n * (e.g. `/(a+)+$/`) are a ReDoS risk when validating untrusted input in server-side contexts.\n * Prefer well-tested, bounded patterns for user-facing validation.\n *\n */\n regex(pattern: RegExp, message?: MessageFn<{ value: string }>): this {\n const safePattern = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ''));\n\n return this._addConstraint(\n (value, ctx) => {\n // Caller-supplied regexes still run against untrusted strings; spell can\n // neutralize stateful /g and /y flags but cannot make arbitrary patterns\n // immune to catastrophic backtracking without breaking the API.\n if (safePattern.test(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.regex, { value: value as string }),\n {\n pattern: safePattern.source,\n },\n );\n },\n (current) => {\n const ann = current as StringAnnotations;\n\n if (ann.pattern === null) return ann; // already ambiguous\n\n if (ann.pattern === undefined) return { ...ann, pattern: safePattern.source };\n\n if (ann.pattern !== safePattern.source) {\n warn(\n '[spell] Multiple .regex() constraints detected on a single string schema. ' +\n 'JSON Schema `pattern` cannot represent multiple patterns and will be omitted from definition output.',\n );\n\n return { ...ann, pattern: null };\n }\n\n return ann;\n },\n );\n }\n\n email(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isEmail(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.email, { value: value as string }),\n { format: 'email' },\n );\n },\n (ann) => ({ ...ann, format: 'email' }),\n );\n }\n\n url(options: UrlOptions = {}): this {\n const { message, protocols = ['http', 'https'] } = options;\n\n return this._addConstraint(\n (value, ctx) => {\n if (isUrl(value as string, protocols)) return null;\n\n return fail(\n ErrorCode.invalid_url,\n resolveMessage(message ?? ctx!.messages.string.url, { value: value as string }),\n { format: 'url' },\n );\n },\n (ann) => ({ ...ann, format: 'uri' }),\n );\n }\n\n uuid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isUuid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.uuid, { value: value as string }),\n { format: 'uuid' },\n );\n },\n (ann) => ({ ...ann, format: 'uuid' }),\n );\n }\n\n isoDate(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isIsoDate(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.date, { value: value as string }),\n {\n format: 'iso-date',\n },\n );\n },\n (ann) => ({ ...ann, format: 'date' }),\n );\n }\n\n isoDateTime(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isIsoDateTime(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.dateTime, { value: value as string }),\n {\n format: 'iso-datetime',\n },\n );\n },\n (ann) => ({ ...ann, format: 'date-time' }),\n );\n }\n\n ip(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isIp(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.ip, { value: value as string }),\n { format: 'ip' },\n );\n });\n }\n\n cuid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isCuid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.cuid, { value: value as string }),\n { format: 'cuid' },\n );\n });\n }\n\n cuid2(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isCuid2(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.cuid2, { value: value as string }),\n { format: 'cuid2' },\n );\n });\n }\n\n ulid(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isUlid(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.ulid, { value: value as string }),\n { format: 'ulid' },\n );\n });\n }\n\n nanoid(length?: number, message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isNanoid(value as string, length)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.nanoid, { value: value as string }),\n { format: 'nanoid' },\n );\n });\n }\n\n base64(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isBase64(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_base64,\n resolveMessage(message ?? ctx!.messages.string.base64, { value: value as string }),\n {\n format: 'base64',\n },\n );\n },\n (ann) => ({ ...ann, contentEncoding: 'base64' }),\n );\n }\n\n base64url(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isBase64url(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_base64,\n resolveMessage(message ?? ctx!.messages.string.base64url, { value: value as string }),\n {\n format: 'base64url',\n },\n );\n });\n }\n\n hex(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isHex(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.hex, { value: value as string }),\n { format: 'hex' },\n );\n });\n }\n\n hexColor(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isHexColor(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.hexColor, { value: value as string }),\n {\n format: 'hex-color',\n },\n );\n });\n }\n\n emoji(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isEmoji(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.emoji, { value: value as string }),\n { format: 'emoji' },\n );\n });\n }\n\n jwt(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isJwt(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.jwt, { value: value as string }),\n { format: 'jwt' },\n );\n });\n }\n\n time(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isTime(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.time, { value: value as string }),\n { format: 'time' },\n );\n });\n }\n\n duration(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint(\n (value, ctx) => {\n if (isDuration(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_duration,\n resolveMessage(message ?? ctx!.messages.string.duration, { value: value as string }),\n {\n format: 'duration',\n },\n );\n },\n (ann) => ({ ...ann, format: 'duration' }),\n );\n }\n\n semver(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isSemver(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.semver, { value: value as string }),\n { format: 'semver' },\n );\n });\n }\n\n slug(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isSlug(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.slug, { value: value as string }),\n { format: 'slug' },\n );\n });\n }\n\n numeric(message?: MessageFn<{ value: string }>): this {\n return this._addConstraint((value, ctx) => {\n if (isNumeric(value as string)) return null;\n\n return fail(\n ErrorCode.invalid_string,\n resolveMessage(message ?? ctx!.messages.string.numeric, { value: value as string }),\n { format: 'numeric' },\n );\n });\n }\n\n /**\n * **Note:** `trim()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n trim(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.trim() : v));\n }\n\n /**\n * **Note:** `lowercase()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n lowercase(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.toLowerCase() : v));\n }\n\n /**\n * **Note:** `uppercase()` adds a preprocessor. Preprocessors are not serializable :\n * `definition()` throws because preprocessors are not serializable.\n */\n uppercase(): this {\n return this.preprocess((v: unknown) => (typeof v === 'string' ? v.toUpperCase() : v));\n }\n\n protected override _toDescriptorImpl(): SchemaDescriptor {\n const ann = this._annotations as StringAnnotations;\n\n return {\n ...this._describeBase(),\n ...(ann.contentEncoding !== undefined ? { contentEncoding: ann.contentEncoding } : {}),\n ...(ann.format !== undefined ? { format: ann.format } : {}),\n ...(ann.maxLength !== undefined ? { maxLength: ann.maxLength } : {}),\n ...(ann.minLength !== undefined ? { minLength: ann.minLength } : {}),\n ...(ann.pattern !== undefined ? { pattern: ann.pattern } : {}),\n kind: 'string',\n };\n }\n\n static coerce(): StringSchema<unknown> {\n return new StringSchema().preprocess((v: unknown) => (v == null ? v : String(v)));\n }\n}\n"],"mappings":"A,4cA0CA,IAAaA,EAAb,MAAaA,UAAqCC,CAAsB,CACtE,IAAuB,OAAgB,CACrC,MAAO,QACT,CAEA,aAAc,CACZ,OAAO,EAAO,IACZ,OAAOC,GAAU,SAAW,KAAOC,EAAKC,EAAU,aAAcC,EAAK,SAAS,OAAO,KAAK,CAAC,CAC7F,CACF,CAEA,IAAI,EAAgB,EAA2D,CAC7E,OAAO,KAAK,gBACT,EAAO,IACDH,EAAiB,QAAUI,EAAe,KAExCH,EACLC,EAAU,UACVG,EAAeC,GAAWH,EAAK,SAAS,OAAO,IAAK,CAAE,IAAKC,EAAe,OAAgB,CAAC,EAC3F,CACE,IAAKA,CACP,CACF,EAED,GAAY,CACX,IAAMG,EAAMC,EAEZ,MAAO,CACL,GAAGD,EACH,UAAWA,EAAI,YAAc,IAAA,GAAYH,EAAS,KAAK,IAAIG,EAAI,UAAWH,CAAM,CAClF,CACF,CACF,CACF,CAEA,IAAI,EAAgB,EAA2D,CAC7E,OAAO,KAAK,gBACT,EAAO,IACDJ,EAAiB,QAAUI,EAAe,KAExCH,EACLC,EAAU,QACVG,EAAeC,GAAWH,EAAK,SAAS,OAAO,IAAK,CAAE,IAAKC,EAAe,OAAgB,CAAC,EAC3F,CACE,IAAKA,CACP,CACF,EAED,GAAY,CACX,IAAMG,EAAMC,EAEZ,MAAO,CACL,GAAGD,EACH,UAAWA,EAAI,YAAc,IAAA,GAAYH,EAAS,KAAK,IAAIG,EAAI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,CAAC,CAAE,CAClF,CACF,E"}
|
|
1
|
+
{"version":3,"file":"string.js","names":["is.isEmail","is.isUrl","is.isUuid","is.isIsoDate","is.isIsoDateTime","is.isTime","is.isDuration","is.isIp","is.isCuid","is.isCuid2","is.isUlid","is.isNanoid","is.isBase64","is.isBase64url","is.isHex","is.isHexColor","is.isEmoji","is.isJwt","is.isSemver","is.isSlug","is.isNumeric","escapeRegExp","text","StringSchema","Schema","value","ok","invalid","issue","ErrorCode","ctx","test","resolveMessage","message","m","format","meta","current","fail","pattern","length","atLeast","atMost","exact","prefix","suffix","substr","safe","isEmail","options","protocols","isUrl","isUuid","isIsoDate","isIsoDateTime","isTime","isDuration","isIp","isCuid","isCuid2","isUlid","isNanoid","isBase64","isBase64url","isHex","isHexColor","isEmoji","isJwt","isSemver","isSlug","isNumeric"],"sources":["../../src/schemas/string.ts"],"sourcesContent":["import {\n atLeast,\n atMost,\n invalid,\n issue,\n type MaybePromise,\n ok,\n type ParseValue,\n type RunContext,\n resolveMessage,\n} from '../_parse';\nimport type { Messages } from '../messages';\nimport * as is from '../predicates';\nimport { Schema } from '../schema';\nimport {\n ErrorCode,\n type Issue,\n type MessageFn,\n type PatternDescriptor,\n type SchemaDescriptor,\n type SchemaKind,\n} from '../types';\n\ntype StringMeta = {\n formats: readonly string[];\n maxLength: number;\n minLength: number;\n patterns: readonly PatternDescriptor[];\n};\n\ntype ValueMessage = MessageFn<{ value: string }>;\n\n/** Each format is a `StringSchema` method, a `Messages['string']` key, and the issue's `params.format`. */\ntype StringFormat =\n | 'base64'\n | 'base64url'\n | 'cuid'\n | 'cuid2'\n | 'duration'\n | 'email'\n | 'emoji'\n | 'hex'\n | 'hexColor'\n | 'ip'\n | 'isoDate'\n | 'isoDateTime'\n | 'jwt'\n | 'nanoid'\n | 'numeric'\n | 'semver'\n | 'slug'\n | 'time'\n | 'ulid'\n | 'url'\n | 'uuid';\n\nconst escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport class StringSchema<Input = string> extends Schema<string, Input> {\n override get kind(): SchemaKind {\n return 'string';\n }\n\n protected override _parse(value: unknown, ctx: RunContext): MaybePromise<ParseValue> {\n return typeof value === 'string'\n ? ok(value)\n : invalid(value, issue(ErrorCode.invalid_type, ctx.messages.string.type()));\n }\n\n private _format(\n format: StringFormat,\n test: (value: string) => boolean,\n message: ValueMessage | undefined,\n meta?: (current: Partial<StringMeta>) => Partial<StringMeta>,\n ): this {\n return this._refine<StringMeta>(\n test,\n (value, m) => issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string[format], { value }), { format }),\n (current) => ({ ...meta?.(current), formats: [...(current.formats ?? []), format] }),\n );\n }\n\n private _pattern(\n test: (value: string) => boolean,\n pattern: PatternDescriptor,\n fail: (value: string, messages: Messages) => Issue,\n ): this {\n return this._refine<StringMeta>(test, fail, (current) => ({ patterns: [...(current.patterns ?? []), pattern] }));\n }\n\n /* -------------------- Length -------------------- */\n\n min(length: number, message?: MessageFn<{ min: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length >= length,\n (value, m) =>\n issue(ErrorCode.too_small, resolveMessage(message ?? m.string.min, { min: length, value }), { min: length }),\n (current) => ({ minLength: atLeast(current.minLength, length) }),\n );\n }\n\n max(length: number, message?: MessageFn<{ max: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length <= length,\n (value, m) =>\n issue(ErrorCode.too_big, resolveMessage(message ?? m.string.max, { max: length, value }), { max: length }),\n (current) => ({ maxLength: atMost(current.maxLength, length) }),\n );\n }\n\n length(exact: number, message?: MessageFn<{ exact: number; value: string }>): this {\n return this._refine<StringMeta>(\n (value) => value.length === exact,\n (value, m) =>\n issue(ErrorCode.invalid_length, resolveMessage(message ?? m.string.length, { exact, value }), { exact }),\n (current) => ({ maxLength: atMost(current.maxLength, exact), minLength: atLeast(current.minLength, exact) }),\n );\n }\n\n nonEmpty(message?: ValueMessage): this {\n return this._refine<StringMeta>(\n (value) => value.length > 0,\n (value, m) => issue(ErrorCode.too_small, resolveMessage(message ?? m.string.nonEmpty, { value }), { min: 1 }),\n (current) => ({ minLength: atLeast(current.minLength, 1) }),\n );\n }\n\n /* -------------------- Patterns -------------------- */\n\n startsWith(prefix: string, message?: MessageFn<{ prefix: string; value: string }>): this {\n return this._pattern(\n (value) => value.startsWith(prefix),\n { flags: '', source: `^${escapeRegExp(prefix)}` },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.startsWith, { prefix, value }), {\n format: 'startsWith',\n prefix,\n }),\n );\n }\n\n endsWith(suffix: string, message?: MessageFn<{ suffix: string; value: string }>): this {\n return this._pattern(\n (value) => value.endsWith(suffix),\n { flags: '', source: `${escapeRegExp(suffix)}$` },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.endsWith, { suffix, value }), {\n format: 'endsWith',\n suffix,\n }),\n );\n }\n\n includes(substr: string, message?: MessageFn<{ substr: string; value: string }>): this {\n return this._pattern(\n (value) => value.includes(substr),\n { flags: '', source: escapeRegExp(substr) },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.includes, { substr, value }), {\n format: 'includes',\n includes: substr,\n }),\n );\n }\n\n /**\n * Validates against `pattern`. Stateful `g`/`y` flags are dropped so repeated parses agree.\n *\n * **Security:** caller-supplied patterns with catastrophic backtracking (e.g. `/(a+)+$/`) are a\n * ReDoS risk on untrusted input; prefer bounded patterns.\n */\n regex(pattern: RegExp, message?: ValueMessage): this {\n const safe = new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ''));\n\n return this._pattern(\n (value) => safe.test(value),\n { flags: safe.flags, source: safe.source },\n (value, m) =>\n issue(ErrorCode.invalid_format, resolveMessage(message ?? m.string.regex, { value }), {\n format: 'regex',\n pattern: safe.source,\n }),\n );\n }\n\n /* -------------------- Formats -------------------- */\n\n email(message?: ValueMessage): this {\n return this._format('email', is.isEmail, message);\n }\n\n /** Absolute URL whose protocol is in `protocols` (default `http`, `https`). */\n url(options: { message?: ValueMessage; protocols?: readonly string[] } = {}): this {\n const { message, protocols = ['http', 'https'] } = options;\n const pattern = { flags: 'i', source: `^(?:${protocols.map(escapeRegExp).join('|')}):` };\n\n return this._format(\n 'url',\n (value) => is.isUrl(value, protocols),\n message,\n (current) => ({\n patterns: [...(current.patterns ?? []), pattern],\n }),\n );\n }\n\n uuid(message?: ValueMessage): this {\n return this._format('uuid', is.isUuid, message);\n }\n\n isoDate(message?: ValueMessage): this {\n return this._format('isoDate', is.isIsoDate, message);\n }\n\n isoDateTime(message?: ValueMessage): this {\n return this._format('isoDateTime', is.isIsoDateTime, message);\n }\n\n time(message?: ValueMessage): this {\n return this._format('time', is.isTime, message);\n }\n\n duration(message?: ValueMessage): this {\n return this._format('duration', is.isDuration, message);\n }\n\n ip(message?: ValueMessage): this {\n return this._format('ip', is.isIp, message);\n }\n\n cuid(message?: ValueMessage): this {\n return this._format('cuid', is.isCuid, message);\n }\n\n cuid2(message?: ValueMessage): this {\n return this._format('cuid2', is.isCuid2, message);\n }\n\n ulid(message?: ValueMessage): this {\n return this._format('ulid', is.isUlid, message);\n }\n\n /** NanoID of exactly `length` characters (default 21). */\n nanoid(length = 21, message?: ValueMessage): this {\n return this._format(\n 'nanoid',\n (value) => is.isNanoid(value, length),\n message,\n (current) => ({\n maxLength: atMost(current.maxLength, length),\n minLength: atLeast(current.minLength, length),\n }),\n );\n }\n\n base64(message?: ValueMessage): this {\n return this._format('base64', is.isBase64, message);\n }\n\n base64url(message?: ValueMessage): this {\n return this._format('base64url', is.isBase64url, message);\n }\n\n hex(message?: ValueMessage): this {\n return this._format('hex', is.isHex, message);\n }\n\n hexColor(message?: ValueMessage): this {\n return this._format('hexColor', is.isHexColor, message);\n }\n\n emoji(message?: ValueMessage): this {\n return this._format('emoji', is.isEmoji, message);\n }\n\n jwt(message?: ValueMessage): this {\n return this._format('jwt', is.isJwt, message);\n }\n\n semver(message?: ValueMessage): this {\n return this._format('semver', is.isSemver, message);\n }\n\n slug(message?: ValueMessage): this {\n return this._format('slug', is.isSlug, message);\n }\n\n numeric(message?: ValueMessage): this {\n return this._format('numeric', is.isNumeric, message);\n }\n\n /* -------------------- Normalization (ordered transforms) -------------------- */\n // Constraints chained after a normalizer see the normalized value. Normalizers are\n // transforms, so `definition()` throws for schemas that use them.\n\n trim(): this {\n return this.transform((value) => value.trim()) as unknown as this;\n }\n\n lowercase(): this {\n return this.transform((value) => value.toLowerCase()) as unknown as this;\n }\n\n uppercase(): this {\n return this.transform((value) => value.toUpperCase()) as unknown as this;\n }\n\n protected override _describe(): SchemaDescriptor {\n return { ...this._describeBase(), ...(this.state.meta as Partial<StringMeta>), kind: 'string' };\n }\n}\n"],"mappings":"A,6eAwDA,IAAMqB,EAAgB,GAAyBC,EAAK,QAAQ,sBAAuB,MAAM,EAE5EC,EAAb,cAAkDC,CAAsB,CACtE,IAAa,MAAmB,CAC9B,MAAO,QACT,CAEA,OAA0B,EAAgB,EAA2C,CACnF,OAAO,OAAOC,GAAU,SACpBC,EAAGD,CAAK,EACRE,EAAQF,EAAOG,EAAMC,EAAU,aAAcC,EAAI,SAAS,OAAO,KAAK,CAAC,CAAC,CAC9E,CAEA,QACE,EACA,EACA,EACA,EACM,CACN,OAAO,KAAK,QACVC,GACC,EAAO,IAAMH,EAAMC,EAAU,eAAgBG,EAAeC,GAAWC,EAAE,OAAOC,GAAS,CAAE,OAAM,CAAC,EAAG,CAAE,QAAO,CAAC,EAC/G,IAAa,CAAE,GAAGC,IAAOC,CAAO,EAAG,QAAS,CAAC,GAAIA,EAAQ,SAAW,CAAC,EAAIF,CAAM,CAAE,EACpF,CACF,CAEA,SACE,EACA,EACA,EACM,CACN,OAAO,KAAK,QAAoBJ,EAAMO,EAAO,IAAa,CAAE,SAAU,CAAC,GAAID,EAAQ,UAAY,CAAC,EAAIE,CAAO,CAAE,EAAE,CACjH,CAIA,IAAI,EAAgB,EAA2D,CAC7E,OAAO,KAAK,QACT,GAAUd,EAAM,Q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CAAO,CAC1D,CAEA,IAAI,EAA8B,CAChC,OAAO,KAAK,QAAQ,MAAOnB,EAAUmB,CAAO,CAC9C,CAEA,SAAS,EAA8B,CACrC,OAAO,KAAK,QAAQ,WAAYlB,EAAekB,CAAO,CACxD,CAEA,MAAM,EAA8B,CAClC,OAAO,KAAK,QAAQ,QAASjB,EAAYiB,CAAO,CAClD,CAEA,IAAI,EAA8B,CAChC,OAAO,KAAK,QAAQ,MAAOhB,EAAUgB,CAAO,CAC9C,CAEA,OAAO,EAA8B,CACnC,OAAO,KAAK,QAAQ,SAAUf,EAAae,CAAO,CACpD,CAEA,KAAK,EAA8B,CACjC,OAAO,KAAK,QAAQ,OAAQd,EAAWc,CAAO,CAChD,CAEA,QAAQ,EAA8B,CACpC,OAAO,KAAK,QAAQ,UAAWb,EAAca,CAAO,CACtD,CAMA,MAAa,CACX,OAAO,KAAK,UAAW,GAAUR,EAAM,KAAK,CAAC,CAC/C,CAEA,WAAkB,CAChB,OAAO,KAAK,UAAW,GAAUA,EAAM,YAAY,CAAC,CACtD,CAEA,WAAkB,CAChB,OAAO,KAAK,UAAW,GAAUA,EAAM,YAAY,CAAC,CACtD,CAEA,WAAiD,CAC/C,MAAO,CAAE,GAAG,KAAK,cAAc,EAAG,GAAI,KAAK,MAAM,KAA8B,KAAM,QAAS,CAChG,CACF,E"}
|
package/dist/schemas/tuple.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../
|
|
1
|
+
const e=require("../_parse.cjs"),t=require("../schema.cjs"),n=require("../types.cjs");var r=class r extends t.Schema{items;restSchema;constructor(e,t=null){super(),this.items=Object.freeze([...e]),this.restSchema=t}get kind(){return`tuple`}rest(e){let t=new r(this.items,e);return t.state=this.state,t}_parse(t,r){let{messages:i}=r,a=this.items.length;if(!Array.isArray(t))return e.invalid(t,e.issue(n.ErrorCode.invalid_type,i.tuple.type()));if(this.restSchema===null&&t.length!==a)return e.invalid(t,e.issue(n.ErrorCode.invalid_length,i.tuple.length({exact:a}),{exact:a}));if(t.length<a)return e.invalid(t,e.issue(n.ErrorCode.too_small,i.tuple.min({min:a}),{min:a}));let o=this.restSchema,s=t.map((e,t)=>(t<a?this.items[t]:o)._run(e,r));return e.chain(e.all(s),n=>{let r=[];return{issues:r,typeOk:!0,value:n.map((n,i)=>n.issues.length===0?n.value:(r.push(...e.prefixIssues(n.issues,i)),t[i]))}})}_describe(){return{...this._describeBase(),items:this.items.map(e=>e.definition()),kind:`tuple`,rest:this.restSchema?.definition()??null}}};exports.TupleSchema=r;
|
|
2
2
|
//# sourceMappingURL=tuple.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tuple.cjs","names":["Schema","
|
|
1
|
+
{"version":3,"file":"tuple.cjs","names":["Schema","invalid","issue","ErrorCode","chain","all","prefixIssues","require__parse","require_schema","require_types","TupleSchema","items","restSchema","next","schema","ctx","fixed","value","messages","rest","runs","index","item","issues","results","result"],"sources":["../../src/schemas/tuple.ts"],"sourcesContent":["import {\n all,\n chain,\n invalid,\n issue,\n type MaybePromise,\n type ParseValue,\n prefixIssues,\n type RunContext,\n} from '../_parse';\nimport { Schema } from '../schema';\nimport {\n type AnySchema,\n ErrorCode,\n type InferInput,\n type InferOutput,\n type Issue,\n type SchemaDescriptor,\n type SchemaKind,\n} from '../types';\n\nexport type TupleSchemas = readonly [AnySchema, ...AnySchema[]];\n\ntype InferTuple<T extends TupleSchemas, R extends AnySchema | null> = R extends AnySchema\n ? readonly [...{ [K in keyof T]: InferOutput<T[K]> }, ...InferOutput<R>[]]\n : { readonly [K in keyof T]: InferOutput<T[K]> };\n\ntype InferTupleInput<T extends TupleSchemas, R extends AnySchema | null> = R extends AnySchema\n ? readonly [...{ [K in keyof T]: InferInput<T[K]> }, ...InferInput<R>[]]\n : { readonly [K in keyof T]: InferInput<T[K]> };\n\nexport class TupleSchema<T extends TupleSchemas, R extends AnySchema | null = null> extends Schema<\n InferTuple<T, R>,\n InferTupleInput<T, R>\n> {\n readonly items: T;\n readonly restSchema: R;\n\n constructor(items: T, restSchema: R = null as R) {\n super();\n this.items = Object.freeze([...items]) as unknown as T;\n this.restSchema = restSchema;\n }\n\n override get kind(): SchemaKind {\n return 'tuple';\n }\n\n /** Accepts any number of trailing items matching `schema`. Keeps this tuple's checks and modifiers. */\n rest<U extends AnySchema>(schema: U): TupleSchema<T, U> {\n const next = new TupleSchema(this.items, schema);\n\n next.state = this.state;\n\n return next;\n }\n\n protected override _parse(value: unknown, ctx: RunContext): MaybePromise<ParseValue> {\n const { messages } = ctx;\n const fixed = this.items.length;\n\n if (!Array.isArray(value)) return invalid(value, issue(ErrorCode.invalid_type, messages.tuple.type()));\n\n if (this.restSchema === null && value.length !== fixed) {\n return invalid(value, issue(ErrorCode.invalid_length, messages.tuple.length({ exact: fixed }), { exact: fixed }));\n }\n\n if (value.length < fixed) {\n return invalid(value, issue(ErrorCode.too_small, messages.tuple.min({ min: fixed }), { min: fixed }));\n }\n\n const rest = this.restSchema;\n const runs = value.map((item, index) => (index < fixed ? this.items[index]! : rest!)._run(item, ctx));\n\n return chain(all(runs), (results) => {\n const issues: Issue[] = [];\n const output = results.map((result, index) => {\n if (result.issues.length === 0) return result.value;\n\n issues.push(...prefixIssues(result.issues, index));\n\n return value[index];\n });\n\n return { issues, typeOk: true, value: output };\n });\n }\n\n protected override _describe(): SchemaDescriptor {\n return {\n ...this._describeBase(),\n items: this.items.map((schema) => schema.definition()),\n kind: 'tuple',\n rest: this.restSchema?.definition() ?? null,\n };\n }\n}\n"],"mappings":"A,sFA+BA,IAAaU,EAAb,MAAaA,UAA+EV,EAAAA,MAG1F,CACA,MACA,WAEA,YAAY,EAAU,EAAgB,KAAW,CAC/C,MAAM,EACN,KAAK,MAAQ,OAAO,OAAO,CAAC,GAAGW,CAAK,CAAC,EACrC,KAAK,WAAaC,CACpB,CAEA,IAAa,MAAmB,CAC9B,MAAO,OACT,CAGA,KAA0B,EAA8B,CACtD,IAAMC,EAAO,IAAIH,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ACN,KAAM,KAAK,YAAY,WAAW,GAAK,IACzC,CACF,CACF,E"}
|
package/dist/schemas/tuple.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Schema } from '../
|
|
1
|
+
import { type MaybePromise, type ParseValue, type RunContext } from '../_parse';
|
|
2
|
+
import { Schema } from '../schema';
|
|
3
|
+
import { type AnySchema, type InferInput, type InferOutput, type SchemaDescriptor, type SchemaKind } from '../types';
|
|
3
4
|
export type TupleSchemas = readonly [AnySchema, ...AnySchema[]];
|
|
4
|
-
|
|
5
|
+
type InferTuple<T extends TupleSchemas, R extends AnySchema | null> = R extends AnySchema ? readonly [...{
|
|
5
6
|
[K in keyof T]: InferOutput<T[K]>;
|
|
6
7
|
}, ...InferOutput<R>[]] : {
|
|
7
8
|
readonly [K in keyof T]: InferOutput<T[K]>;
|
|
8
9
|
};
|
|
9
|
-
type InferTupleInput<T extends TupleSchemas, R extends AnySchema | null
|
|
10
|
+
type InferTupleInput<T extends TupleSchemas, R extends AnySchema | null> = R extends AnySchema ? readonly [...{
|
|
10
11
|
[K in keyof T]: InferInput<T[K]>;
|
|
11
12
|
}, ...InferInput<R>[]] : {
|
|
12
13
|
readonly [K in keyof T]: InferInput<T[K]>;
|
|
@@ -14,13 +15,12 @@ type InferTupleInput<T extends TupleSchemas, R extends AnySchema | null = null>
|
|
|
14
15
|
export declare class TupleSchema<T extends TupleSchemas, R extends AnySchema | null = null> extends Schema<InferTuple<T, R>, InferTupleInput<T, R>> {
|
|
15
16
|
readonly items: T;
|
|
16
17
|
readonly restSchema: R;
|
|
17
|
-
protected get _kind(): string;
|
|
18
18
|
constructor(items: T, restSchema?: R);
|
|
19
|
+
get kind(): SchemaKind;
|
|
20
|
+
/** Accepts any number of trailing items matching `schema`. Keeps this tuple's checks and modifiers. */
|
|
19
21
|
rest<U extends AnySchema>(schema: U): TupleSchema<T, U>;
|
|
20
|
-
|
|
21
|
-
protected
|
|
22
|
-
protected _parseAsync(value: unknown, ctx: ParseContext): Promise<ParseValue>;
|
|
23
|
-
protected _toDescriptorImpl(): SchemaDescriptor;
|
|
22
|
+
protected _parse(value: unknown, ctx: RunContext): MaybePromise<ParseValue>;
|
|
23
|
+
protected _describe(): SchemaDescriptor;
|
|
24
24
|
}
|
|
25
25
|
export {};
|
|
26
26
|
//# sourceMappingURL=tuple.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../../src/schemas/tuple.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"tuple.d.ts","sourceRoot":"","sources":["../../src/schemas/tuple.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,YAAY,EACjB,KAAK,UAAU,EAEf,KAAK,UAAU,EAChB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,UAAU,EACf,KAAK,WAAW,EAEhB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,UAAU,CAAC;AAElB,MAAM,MAAM,YAAY,GAAG,SAAS,CAAC,SAAS,EAAE,GAAG,SAAS,EAAE,CAAC,CAAC;AAEhE,KAAK,UAAU,CAAC,CAAC,SAAS,YAAY,EAAE,CAAC,SAAS,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,SAAS,GACrF,SAAS,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,GACxE;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC;AAEnD,KAAK,eAAe,CAAC,CAAC,SAAS,YAAY,EAAE,CAAC,SAAS,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,SAAS,GAC1F,SAAS,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,EAAE,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,GACtE;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC;AAElD,qBAAa,WAAW,CAAC,CAAC,SAAS,YAAY,EAAE,CAAC,SAAS,SAAS,GAAG,IAAI,GAAG,IAAI,CAAE,SAAQ,MAAM,CAChG,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EAChB,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CACtB;IACC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;gBAEX,KAAK,EAAE,CAAC,EAAE,UAAU,GAAE,CAAa;IAM/C,IAAa,IAAI,IAAI,UAAU,CAE9B;IAED,uGAAuG;IACvG,IAAI,CAAC,CAAC,SAAS,SAAS,EAAE,MAAM,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;cAQpC,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,GAAG,YAAY,CAAC,UAAU,CAAC;cA+BjE,SAAS,IAAI,gBAAgB;CAQjD"}
|
package/dist/schemas/tuple.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{all as e,chain as t,invalid as n,issue as r,prefixIssues as i}from"../_parse.js";import{Schema as a}from"../schema.js";import{ErrorCode as o}from"../types.js";var s=class s extends a{items;restSchema;constructor(e,t=null){super(),this.items=Object.freeze([...e]),this.restSchema=t}get kind(){return`tuple`}rest(e){let t=new s(this.items,e);return t.state=this.state,t}_parse(a,s){let{messages:c}=s,l=this.items.length;if(!Array.isArray(a))return n(a,r(o.invalid_type,c.tuple.type()));if(this.restSchema===null&&a.length!==l)return n(a,r(o.invalid_length,c.tuple.length({exact:l}),{exact:l}));if(a.length<l)return n(a,r(o.too_small,c.tuple.min({min:l}),{min:l}));let u=this.restSchema,d=a.map((e,t)=>(t<l?this.items[t]:u)._run(e,s));return t(e(d),e=>{let t=[];return{issues:t,typeOk:!0,value:e.map((e,n)=>e.issues.length===0?e.value:(t.push(...i(e.issues,n)),a[n]))}})}_describe(){return{...this._describeBase(),items:this.items.map(e=>e.definition()),kind:`tuple`,rest:this.restSchema?.definition()??null}}};export{s as TupleSchema};
|
|
2
2
|
//# sourceMappingURL=tuple.js.map
|