json-api-nestjs
Version:
JsonApi Plugin for NestJs
110 lines • 3.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.zodAttributes = zodAttributes;
const nestjs_shared_1 = require("../../../../utils/nestjs-shared");
const zod_1 = require("zod");
const types_1 = require("../../types");
const zod_utils_1 = require("../zod-utils");
const literalSchema = zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()]);
function getZodRulesForNumber(isNullable) {
const schema = zod_1.z.preprocess((x) => Number(x), zod_1.z.number());
if (isNullable)
schema.nullable().optional();
return isNullable ? schema.optional() : schema;
}
function getZodRulesForString(isNullable) {
const schema = zod_1.z.string();
if (isNullable)
schema.nullable().optional();
return isNullable ? schema.optional() : schema;
}
function getZodRulesForDate(isNullable) {
const schema = zod_1.z.coerce.date();
if (isNullable)
schema.nullable().optional();
return isNullable ? schema.optional() : schema;
}
function getZodRulesForBoolean(isNullable) {
const schema = zod_1.z.boolean();
if (isNullable)
schema.nullable();
return isNullable ? schema.optional() : schema;
}
function getZodSchemaForJson(isNullable) {
const tmpSchema = isNullable ? literalSchema.nullable() : literalSchema;
const schema = zod_1.z.lazy(() => zod_1.z.union([tmpSchema, zod_1.z.array(tmpSchema), zod_1.z.record(tmpSchema)]));
return isNullable ? schema.optional() : schema;
}
function getZodRulesForArray(propsField) {
const type = propsField.type;
let schema;
if (!propsField) {
schema = getZodRulesForString(false);
}
else {
switch (type) {
case 'number':
case 'real':
case 'integer':
case 'bigint':
case 'double':
case 'numeric':
case Number:
schema = getZodRulesForNumber(false);
break;
case 'date':
case Date:
schema = getZodRulesForDate(false);
break;
case 'boolean':
case Boolean:
schema = getZodRulesForBoolean(false);
break;
default:
schema = getZodRulesForString(false);
}
}
if (propsField.isNullable) {
return schema.array().nullable();
}
return schema.array();
}
function buildSchema(fieldType, propsField) {
let schema;
switch (fieldType) {
case types_1.TypeField.array:
schema = getZodRulesForArray(propsField);
break;
case types_1.TypeField.date:
schema = getZodRulesForDate(propsField.isNullable);
break;
case types_1.TypeField.boolean:
schema = getZodRulesForBoolean(propsField.isNullable);
break;
case types_1.TypeField.number:
schema = getZodRulesForNumber(propsField.isNullable);
break;
case types_1.TypeField.object:
schema = getZodSchemaForJson(propsField.isNullable);
break;
default:
schema = getZodRulesForString(propsField.isNullable);
}
return schema;
}
function zodAttributes(fieldWithType, propsDb, primaryColumn, isPatch) {
const objectShape = {};
for (const [nameList, type] of nestjs_shared_1.ObjectTyped.entries(fieldWithType)) {
if (nameList === primaryColumn)
continue;
const name = nameList;
const propsField = propsDb[name];
objectShape[name] = buildSchema(type, propsField || {});
}
const zodSchema = zod_1.z.object(objectShape).strict();
if (isPatch) {
return zodSchema.partial().refine((0, zod_utils_1.nonEmptyObject)());
}
return zodSchema.refine((0, zod_utils_1.nonEmptyObject)());
}
//# sourceMappingURL=attributes.js.map