zod
Version:
Typescript-first schema declaration and validation library with static type inference
129 lines • 4.94 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var z = __importStar(require("./types/base"));
exports.ZodParser = function (schemaDef) { return function (obj) {
var def = schemaDef;
switch (def.t) {
case z.ZodTypes.string:
if (typeof obj !== 'string')
throw new Error("Non-string type: " + typeof obj);
return obj;
case z.ZodTypes.number:
if (typeof obj !== 'number')
throw new Error("Non-number type: " + typeof obj);
if (Number.isNaN(obj)) {
throw new Error("Non-number type: NaN");
}
return obj;
case z.ZodTypes.boolean:
if (typeof obj !== 'boolean')
throw new Error("Non-boolean type: " + typeof obj);
return obj;
case z.ZodTypes.undefined:
if (obj !== undefined)
throw new Error("Non-undefined type:Found: " + typeof obj);
return obj;
case z.ZodTypes.null:
if (obj !== null)
throw new Error("Non-null type: " + typeof obj);
return obj;
case z.ZodTypes.array:
if (!Array.isArray(obj))
throw new Error("Non-array type: " + typeof obj);
var arrayErrors_1 = [];
if (def.nonempty === true && obj.length === 0) {
throw new Error('Array cannot be empty');
}
var parsedArray = obj.map(function (item, i) {
try {
var parsedItem = def.type.parse(item);
return parsedItem;
}
catch (err) {
arrayErrors_1.push("[" + i + "]: " + err.message);
return null;
}
});
if (arrayErrors_1.length > 0) {
// throw new Error(arrayErrors.join('\n\n'));
throw new Error(arrayErrors_1.join('\n'));
}
return parsedArray;
case z.ZodTypes.object:
if (typeof obj !== 'object')
throw new Error("Non-object type: " + typeof obj);
if (Array.isArray(obj))
throw new Error("Non-object type: array");
var shape = def.shape;
var objectErrors = [];
for (var key in shape) {
try {
def.shape[key].parse(obj[key]);
}
catch (err) {
objectErrors.push(key + ": " + err.message);
}
}
if (Object.keys(objectErrors).length > 0) {
throw new Error(objectErrors.join('\n'));
}
return obj;
case z.ZodTypes.union:
for (var _i = 0, _a = def.options; _i < _a.length; _i++) {
var option = _a[_i];
try {
option.parse(obj);
return obj;
}
catch (err) { }
}
throw new Error("Type mismatch in union.\nReceived: " + JSON.stringify(obj, null, 2) + "\n\nExpected: " + def.options
.map(function (x) { return x._def.t; })
.join(' OR '));
case z.ZodTypes.intersection:
var errors = [];
try {
def.left.parse(obj);
}
catch (err) {
errors.push("Left side of intersection: " + err.message);
}
try {
def.right.parse(obj);
}
catch (err) {
errors.push("Right side of intersection: " + err.message);
}
if (!errors.length) {
return obj;
}
throw new Error(errors.join('\n'));
case z.ZodTypes.tuple:
if (!Array.isArray(obj)) {
throw new Error('Non-array type detected; invalid tuple.');
}
if (def.items.length !== obj.length) {
throw new Error("Incorrect number of elements in tuple: expected " + def.items.length + ", got " + obj.length);
}
var parsedTuple = [];
for (var index in obj) {
var item = obj[index];
parsedTuple.push(def.items[index].parse(item));
}
return parsedTuple;
case z.ZodTypes.lazy:
var lazySchema = def.getter();
lazySchema.parse(obj);
return obj;
default:
throw new Error("Invalid schema type: " + def.t);
}
}; };
//# sourceMappingURL=parser.js.map