Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 103867x 103867x 103867x 51467x 51467x 51467x 45320x 103867x 193x 193x 193x 193x 193x 193x 193x 67659x 67659x 52336x 52336x 15323x 67659x 2057x 2057x 2057x 13266x 67659x 193x 193x 193x 193x 193x 193x 193x 193x 53186x 53186x 53186x 111011x 111011x 8768x 8768x 111011x 40868x 40868x 40868x 111011x 16957x 16957x 16957x 44418x 44418x 44418x 53186x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 65072x 65072x 65072x 117268x 117268x 17532x 17532x 117268x 34310x 34310x 34310x 65426x 117268x 17886x 17886x 17886x 47540x 47540x 47540x 65072x | /**
* @typedef {Object} ZodSchemaLike
* @property {{ type?: string, innerType?: ZodSchemaLike, defaultValue?: unknown, typeName?: string, in?: ZodSchemaLike }} [_def]
* @property {string[]} [options]
*/
/**
* Checks if a Zod schema is a boolean.
* Handles wrapped schemas (default, optional, nullable).
* @param {ZodSchemaLike} schema
* @return {boolean}
*/
export function isBoolean(schema) {
const schemaType = schema._def?.type;
if (schemaType === 'boolean') return true;
if (schemaType === 'default' || schemaType === 'optional' || schemaType === 'nullable') {
const innerType = schema._def?.innerType;
if (innerType) return isBoolean(innerType);
}
return false;
}
/**
* Checks if a Zod schema is required (no default, not optional, not nullable).
* @param {ZodSchemaLike} schema
* @return {boolean}
*/
export function isRequired(schema) {
const schemaType = schema._def?.type;
if (schemaType === 'default' || schemaType === 'optional' || schemaType === 'nullable') {
return false;
}
// For pipe schemas (created by .transform()), check the input schema
if (schemaType === 'pipe') {
const inputSchema = schema._def?.in;
if (inputSchema) return isRequired(inputSchema);
}
return true;
}
/**
* Extracts enum values from a Zod schema.
* Handles wrapped schemas (default, optional, nullable, pipe).
* @param {ZodSchemaLike} schema
* @return {string[] | null}
*/
export function getEnumValues(schema) {
/** @type {ZodSchemaLike | undefined} */
let current = schema;
while (current) {
const schemaType = current._def?.type;
if (schemaType === 'enum') {
return current.options ?? null;
}
if (schemaType === 'default' || schemaType === 'optional' || schemaType === 'nullable') {
current = current._def?.innerType;
continue;
}
if (schemaType === 'pipe') {
current = current._def?.in;
continue;
}
break;
}
return null;
}
/**
* Unwraps optional/nullable/default/pipe wrappers to get the inner schema.
* @param {ZodSchemaLike} schema
* @return {ZodSchemaLike}
*/
export function unwrapSchema(schema) {
/** @type {ZodSchemaLike | undefined} */
let current = schema;
while (current) {
const schemaType = current._def?.type;
if (schemaType === 'optional' || schemaType === 'nullable' || schemaType === 'default') {
current = current._def?.innerType;
continue;
}
if (schemaType === 'pipe') {
current = current._def?.in;
continue;
}
break;
}
return /** @type {ZodSchemaLike} */ (current);
}
/**
* Extracts the default value from a Zod schema.
* Handles wrapped schemas (optional, nullable, pipe).
* @param {ZodSchemaLike} schema
* @return {unknown}
*/
export function getDefault(schema) {
/** @type {ZodSchemaLike | undefined} */
let current = schema;
while (current) {
const schemaType = current._def?.type;
if (schemaType === 'default') {
return current._def?.defaultValue;
}
if (schemaType === 'optional' || schemaType === 'nullable') {
current = current._def?.innerType;
continue;
}
// For pipe schemas (created by .transform()), check the input schema
if (schemaType === 'pipe') {
current = current._def?.in;
continue;
}
break;
}
return undefined;
}
|