UNPKG

json-api-nestjs

Version:
42 lines 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.zodFieldsInputQuery = zodFieldsInputQuery; const nestjs_shared_1 = require("../../../../utils/nestjs-shared"); const zod_1 = require("zod"); const zod_utils_1 = require("../zod-utils"); function getZodRules() { return zod_1.z .string() .optional() .transform((input) => (input ? input.split(',') : undefined)); } function zodFieldsInputQuery(relationList) { const target = zod_1.z.object({ target: getZodRules(), }); const relation = relationList.reduce((acum, item) => ({ ...acum, [item]: getZodRules(), }), {}); return target .merge(zod_1.z.object(relation)) .strict((0, zod_utils_1.getValidationErrorForStrict)(['target', ...relationList], 'Fields')) .refine((0, zod_utils_1.nonEmptyObject)(), { message: 'Validation error: Need select field for target or relation', }) .optional() .transform((input) => { if (!input) return null; const result = nestjs_shared_1.ObjectTyped.entries(input).reduce((acum, [key, value]) => { if (!value || value.length === 0) return acum; return { ...acum, [key]: value, }; }, {}); return Object.keys(result).length > 0 ? result : null; }); } //# sourceMappingURL=fields.js.map