zod-error
Version:
Utilities to format and customize Zod error messages
45 lines (44 loc) • 2.13 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 now = new Date();
const schema = zod_1.z.object({
id: zod_1.z
.string()
.uuid()
.transform((value) => __awaiter(void 0, void 0, void 0, function* () {
yield new Promise((res) => setTimeout(res, 1));
return value;
})),
timestamp: zod_1.z.number(),
message: zod_1.z.string().min(5),
});
const invalidData = {
id: 'ID001',
timestamp: now,
message: 'lol!',
};
const validData = {
id: '6511febf-b312-4456-a19a-05ddb86a6b74',
timestamp: now.valueOf(),
message: 'lolol!',
};
test('async safe parse with invalid data return an error object', () => __awaiter(void 0, void 0, void 0, function* () {
return expect((0, _1.safeParseAsync)(schema, invalidData)).resolves.toStrictEqual({
success: false,
error: {
message: 'Code: invalid_string ~ Path: id ~ Message: Invalid uuid | Code: invalid_type ~ Path: timestamp ~ Message: Expected number, received date | Code: too_small ~ Path: message ~ Message: String must contain at least 5 character(s)',
},
});
}));
test('async safe parse with valid data returns a success object', () => __awaiter(void 0, void 0, void 0, function* () { return expect((0, _1.safeParseAsync)(schema, validData)).resolves.toStrictEqual({ success: true, data: validData }); }));