zod-error
Version:
Utilities to format and customize Zod error messages
24 lines (23 loc) • 1.64 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require(".");
const zod_1 = require("zod");
const schema = zod_1.z.object({
animal: zod_1.z.enum(['🐶', '🐱', '🐵']),
quantity: zod_1.z.number().gte(1),
});
const invalidData = { animal: '🐼', quantity: 0 };
const validData = { animal: '🐶', quantity: 1 };
test('parse with invalid throws a generic error', () => __awaiter(void 0, void 0, void 0, function* () {
return expect(() => (0, _1.parse)(schema, invalidData)).toThrowError(new Error("Code: invalid_enum_value ~ Path: animal ~ Message: Invalid enum value. Expected '🐶' | '🐱' | '🐵', received '🐼' | Code: too_small ~ Path: quantity ~ Message: Number must be greater than or equal to 1"));
}));
test('parse with valid data to return the valid data', () => __awaiter(void 0, void 0, void 0, function* () { return expect((0, _1.parse)(schema, validData)).toStrictEqual(validData); }));