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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | 4x 4x 70x 56x 3x 11x 4x 950x 949x 949x 949x 26x 70x 70x 8x 949x 52x 949x 148x 275x 949x 2x 949x 9x 17x 17x 4x 1896x 1896x 1896x 1896x 1896x 1896x 7x 1896x 4x 4x 1x 1896x 4x 4x 1x 1896x 1x 3x 3x 3x 3x 1x 1896x 137x 1896x 334x 619x 1896x 15x 1896x 27x 53x 53x 4x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 3x 1672x 18x 18x 3x 1672x 77x 77x 77x 77x 1x 1x 77x 1672x 23x 23x 1x 23x 1x 23x 1x 1672x 20x 56x 56x 54x 54x 54x 1x 54x 1672x 123x 1672x 312x 587x 1672x 13x 1672x 15x 29x 29x 5x 3x 3x 3x 3x 4x 1896x 1896x 1896x 1896x 1896x 1896x 1896x 4x 1896x 12x 1x 12x 1x 12x 4x 6x 5x 2x 2x 1896x 4x 1896x 4x 8x 8x 1896x 138x 1896x 334x 619x 1896x 15x 1896x 27x 53x 53x 4x 4x 4x 4x 125x 110x 42x 2x 587x 2x 587x 587x 587x 587x 125x | import type { Schema, SchemaKey, Value } from "@featurevisor/types";
import { z } from "zod";
import { refineWithMessage } from "./zodHelpers";
/** Returns true if value matches the schema type. */
function valueMatchesType(v: unknown, type: string): boolean {
switch (type) {
case "string":
return typeof v === "string";
case "boolean":
return typeof v === "boolean";
case "integer":
return typeof v === "number" && Number.isInteger(v);
case "double":
return typeof v === "number";
case "object":
return typeof v === "object" && v !== null && !Array.isArray(v);
case "array":
return Array.isArray(v);
default:
return true;
}
}
type SchemaLike = {
type?: string;
enum?: unknown[];
const?: unknown;
minimum?: number;
maximum?: number;
minLength?: number;
maxLength?: number;
pattern?: string;
minItems?: number;
maxItems?: number;
uniqueItems?: boolean;
items?: unknown;
properties?: Record<string, unknown>;
additionalProperties?: unknown;
oneOf?: unknown[];
};
/**
* Recursively validates that when a schema has both `type` and `enum`, every enum value matches the type.
* Also recurses into oneOf branches.
*/
export function refineEnumMatchesType(
schema: SchemaLike,
pathPrefix: (string | number)[],
ctx: z.RefinementCtx,
): void {
if (!schema || typeof schema !== "object") return;
const type = schema.type;
const enumArr = schema.enum;
if (type && Array.isArray(enumArr) && enumArr.length > 0) {
for (let i = 0; i < enumArr.length; i++) {
const v = enumArr[i];
if (!valueMatchesType(v, type)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} (${JSON.stringify(v)}) does not match type "${type}". All enum values must be of the same type as \`type\`.`,
path: [...pathPrefix, "enum", i],
});
}
}
}
if (schema.items && typeof schema.items === "object") {
refineEnumMatchesType(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
}
if (schema.properties && typeof schema.properties === "object") {
for (const k of Object.keys(schema.properties)) {
refineEnumMatchesType(
schema.properties[k] as SchemaLike,
[...pathPrefix, "properties", k],
ctx,
);
}
}
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
refineEnumMatchesType(
schema.additionalProperties as SchemaLike,
[...pathPrefix, "additionalProperties"],
ctx,
);
}
if (schema.oneOf && Array.isArray(schema.oneOf)) {
schema.oneOf.forEach((branch, i) => {
if (branch && typeof branch === "object") {
refineEnumMatchesType(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
}
});
}
}
/**
* Validates that when a schema has type "integer" or "double", minimum <= maximum when both set,
* and const/enum values (if present) fall within the range.
*/
export function refineMinimumMaximum(
schema: SchemaLike,
pathPrefix: (string | number)[],
ctx: z.RefinementCtx,
): void {
Iif (!schema || typeof schema !== "object") return;
const type = schema.type;
const min = schema.minimum;
const max = schema.maximum;
const isNumeric = type === "integer" || type === "double";
if (isNumeric && min !== undefined && max !== undefined && min > max) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `When \`type\` is "${type}", \`minimum\` (${min}) must be less than or equal to \`maximum\` (${max}).`,
path: [...pathPrefix, "minimum"],
});
}
if (isNumeric && min !== undefined && schema.const !== undefined) {
const v = schema.const;
if (typeof v === "number" && v < min) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`const\` value ${v} is less than \`minimum\` (${min}).`,
path: [...pathPrefix, "const"],
});
}
}
if (isNumeric && max !== undefined && schema.const !== undefined) {
const v = schema.const;
if (typeof v === "number" && v > max) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`const\` value ${v} is greater than \`maximum\` (${max}).`,
path: [...pathPrefix, "const"],
});
}
}
if (isNumeric && Array.isArray(schema.enum) && schema.enum.length > 0) {
for (let i = 0; i < schema.enum.length; i++) {
const v = schema.enum[i];
if (typeof v === "number") {
Iif (min !== undefined && v < min) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} (${v}) is less than \`minimum\` (${min}).`,
path: [...pathPrefix, "enum", i],
});
}
if (max !== undefined && v > max) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} (${v}) is greater than \`maximum\` (${max}).`,
path: [...pathPrefix, "enum", i],
});
}
}
}
}
if (schema.items && typeof schema.items === "object") {
refineMinimumMaximum(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
}
if (schema.properties && typeof schema.properties === "object") {
for (const k of Object.keys(schema.properties)) {
refineMinimumMaximum(
schema.properties[k] as SchemaLike,
[...pathPrefix, "properties", k],
ctx,
);
}
}
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
refineMinimumMaximum(
schema.additionalProperties as SchemaLike,
[...pathPrefix, "additionalProperties"],
ctx,
);
}
if (schema.oneOf && Array.isArray(schema.oneOf)) {
schema.oneOf.forEach((branch, i) => {
if (branch && typeof branch === "object") {
refineMinimumMaximum(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
}
});
}
}
/**
* Validates that when a schema has type "string", minLength <= maxLength when both set,
* pattern is a valid RegExp (if set), and const/enum string values satisfy length and pattern.
*/
export function refineStringLengthPattern(
schema: SchemaLike,
pathPrefix: (string | number)[],
ctx: z.RefinementCtx,
): void {
Iif (!schema || typeof schema !== "object") return;
const type = schema.type;
const minLen = schema.minLength;
const maxLen = schema.maxLength;
const patternStr = schema.pattern;
const isString = type === "string";
if (isString && minLen !== undefined && maxLen !== undefined && minLen > maxLen) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `When \`type\` is "string", \`minLength\` (${minLen}) must be less than or equal to \`maxLength\` (${maxLen}).`,
path: [...pathPrefix, "minLength"],
});
}
if (isString && patternStr !== undefined) {
try {
new RegExp(patternStr);
} catch {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`pattern\` must be a valid ECMA-262 regular expression; "${patternStr}" is invalid.`,
path: [...pathPrefix, "pattern"],
});
}
}
const testString = (s: string): { minOk: boolean; maxOk: boolean; patternOk: boolean } => {
const minOk = minLen === undefined || s.length >= minLen;
const maxOk = maxLen === undefined || s.length <= maxLen;
let patternOk = true;
if (patternStr !== undefined) {
try {
patternOk = new RegExp(patternStr).test(s);
} catch {
patternOk = true;
}
}
return { minOk, maxOk, patternOk };
};
if (isString && schema.const !== undefined && typeof schema.const === "string") {
const { minOk, maxOk, patternOk } = testString(schema.const);
if (!minOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`const\` value length (${schema.const.length}) is less than \`minLength\` (${minLen}).`,
path: [...pathPrefix, "const"],
});
}
if (!maxOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`const\` value length (${schema.const.length}) is greater than \`maxLength\` (${maxLen}).`,
path: [...pathPrefix, "const"],
});
}
if (!patternOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `\`const\` value does not match \`pattern\`.`,
path: [...pathPrefix, "const"],
});
}
}
if (isString && Array.isArray(schema.enum) && schema.enum.length > 0) {
for (let i = 0; i < schema.enum.length; i++) {
const v = schema.enum[i];
if (typeof v === "string") {
const { minOk, maxOk, patternOk } = testString(v);
Iif (!minOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} length (${v.length}) is less than \`minLength\` (${minLen}).`,
path: [...pathPrefix, "enum", i],
});
}
if (!maxOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} length (${v.length}) is greater than \`maxLength\` (${maxLen}).`,
path: [...pathPrefix, "enum", i],
});
}
Iif (!patternOk) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Enum value at index ${i} does not match \`pattern\`.`,
path: [...pathPrefix, "enum", i],
});
}
}
}
}
if (schema.items && typeof schema.items === "object") {
refineStringLengthPattern(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
}
if (schema.properties && typeof schema.properties === "object") {
for (const k of Object.keys(schema.properties)) {
refineStringLengthPattern(
schema.properties[k] as SchemaLike,
[...pathPrefix, "properties", k],
ctx,
);
}
}
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
refineStringLengthPattern(
schema.additionalProperties as SchemaLike,
[...pathPrefix, "additionalProperties"],
ctx,
);
}
if (schema.oneOf && Array.isArray(schema.oneOf)) {
schema.oneOf.forEach((branch, i) => {
if (branch && typeof branch === "object") {
refineStringLengthPattern(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
}
});
}
}
/** Deep equality for primitive/array/object values (used for uniqueItems check). */
function valueDeepEqualForRefine(a: unknown, b: unknown): boolean {
if (a === b) return true;
Iif (typeof a !== typeof b) return false;
Iif (a === null || b === null) return a === b;
Iif (typeof a === "object" && typeof b === "object") {
Iif (Array.isArray(a) !== Array.isArray(b)) return false;
Iif (Array.isArray(a) && Array.isArray(b)) {
Iif (a.length !== b.length) return false;
return a.every((v, i) => valueDeepEqualForRefine(v, b[i]));
}
const keysA = Object.keys(a as object).sort();
const keysB = Object.keys(b as object).sort();
Iif (keysA.length !== keysB.length || keysA.some((k, i) => k !== keysB[i])) return false;
return keysA.every((k) =>
valueDeepEqualForRefine((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k]),
);
}
return false;
}
/**
* Validates that when a schema has type "array", minItems <= maxItems when both set,
* and const/enum array values (if present) satisfy length and uniqueItems.
*/
export function refineArrayItems(
schema: SchemaLike,
pathPrefix: (string | number)[],
ctx: z.RefinementCtx,
): void {
Iif (!schema || typeof schema !== "object") return;
const type = schema.type;
const minItems = schema.minItems;
const maxItems = schema.maxItems;
const uniqueItems = schema.uniqueItems;
const isArray = type === "array";
if (isArray && minItems !== undefined && maxItems !== undefined && minItems > maxItems) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `When \`type\` is "array", \`minItems\` (${minItems}) must be less than or equal to \`maxItems\` (${maxItems}).`,
path: [...pathPrefix, "minItems"],
});
}
const checkArray = (arr: unknown[], pathSuffix: (string | number)[]) => {
if (minItems !== undefined && arr.length < minItems) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Array length (${arr.length}) is less than \`minItems\` (${minItems}).`,
path: pathSuffix,
});
}
if (maxItems !== undefined && arr.length > maxItems) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Array length (${arr.length}) is greater than \`maxItems\` (${maxItems}).`,
path: pathSuffix,
});
}
if (uniqueItems) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (valueDeepEqualForRefine(arr[i], arr[j])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Array has duplicate items at indices ${i} and ${j} but \`uniqueItems\` is true.`,
path: pathSuffix,
});
return;
}
}
}
}
};
if (isArray && Array.isArray(schema.const)) {
checkArray(schema.const, [...pathPrefix, "const"]);
}
if (isArray && Array.isArray(schema.enum) && schema.enum.length > 0) {
for (let i = 0; i < schema.enum.length; i++) {
const v = schema.enum[i];
if (Array.isArray(v)) checkArray(v, [...pathPrefix, "enum", i]);
}
}
if (schema.items && typeof schema.items === "object") {
refineArrayItems(schema.items as SchemaLike, [...pathPrefix, "items"], ctx);
}
if (schema.properties && typeof schema.properties === "object") {
for (const k of Object.keys(schema.properties)) {
refineArrayItems(schema.properties[k] as SchemaLike, [...pathPrefix, "properties", k], ctx);
}
}
if (schema.additionalProperties && typeof schema.additionalProperties === "object") {
refineArrayItems(
schema.additionalProperties as SchemaLike,
[...pathPrefix, "additionalProperties"],
ctx,
);
}
if (schema.oneOf && Array.isArray(schema.oneOf)) {
schema.oneOf.forEach((branch, i) => {
if (branch && typeof branch === "object") {
refineArrayItems(branch as SchemaLike, [...pathPrefix, "oneOf", i], ctx);
}
});
}
}
// Recursive schema for Value: boolean | string | number | ObjectValue | Value[]
export const valueZodSchema: z.ZodType<Value> = z.lazy(() =>
z.union([
z.boolean(),
z.string(),
z.number(),
// | Date // @TODO: support in future
z.record(z.string(), valueZodSchema),
z.array(valueZodSchema),
]),
);
// @TODO: support "date" in future
// @TODO: consider "semver" in future
// @TODO: consider "url" in future
export const propertyTypeEnum = z.enum([
"boolean",
"string",
"integer",
"double",
"object",
"array",
]);
export function getSchemaZodSchema(schemaKeys: SchemaKey[] = []) {
const schemaZodSchema: z.ZodType<Schema> = z.lazy(() =>
z
.object({
description: z.string().optional(),
type: propertyTypeEnum.optional(),
enum: z.array(valueZodSchema).optional(),
const: valueZodSchema.optional(),
minimum: z.number().optional(),
maximum: z.number().optional(),
minLength: z.number().optional(),
maxLength: z.number().optional(),
pattern: z.string().optional(),
items: schemaZodSchema.optional(),
minItems: z.number().optional(),
maxItems: z.number().optional(),
uniqueItems: z.boolean().optional(),
required: z.array(z.string()).optional(),
properties: z.record(z.string(), schemaZodSchema).optional(),
additionalProperties: schemaZodSchema.optional(),
// Annotations: default?: Value; examples?: Value[];
schema: refineWithMessage(
z.string(),
(value) => schemaKeys.includes(value),
(value) => `Unknown schema "${value}"`,
).optional(),
oneOf: z.array(schemaZodSchema).min(2).optional(),
})
.strict()
.superRefine((data, ctx) => {
if (data.type === "array" && data.items === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
'When `type` is "array", `items` is required. Define the schema for array elements.',
path: ["items"],
});
}
})
.superRefine((data, ctx) => refineEnumMatchesType(data, [], ctx))
.superRefine((data, ctx) => refineMinimumMaximum(data, [], ctx))
.superRefine((data, ctx) => refineStringLengthPattern(data, [], ctx))
.superRefine((data, ctx) => refineArrayItems(data, [], ctx)),
);
return schemaZodSchema;
}
|