UNPKG

json-api-nestjs

Version:
157 lines 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZodInputPatchRelationshipSchema = exports.ZodInputPostRelationshipSchema = void 0; exports.getParamsForOatchANdPostZod = getParamsForOatchANdPostZod; exports.ZodInputQuerySchema = ZodInputQuerySchema; exports.ZodQuerySchema = ZodQuerySchema; exports.ZodPostSchema = ZodPostSchema; exports.ZodPatchSchema = ZodPatchSchema; const constants_1 = require("../../../constants"); const zod_1 = require("../zod"); const nestjs_shared_1 = require("../../../utils/nestjs-shared"); function getEntityMap(entityMapProps, entity) { const entityMap = entityMapProps.get(entity); if (!entityMap) throw new Error('Entity not found in map'); return entityMap; } function getParamsForOatchANdPostZod(entityMapProps, entity) { const entityMap = getEntityMap(entityMapProps, entity); const { primaryColumnType, typeName, propsType, primaryColumnName, propsNullable, relationProperty, } = entityMap; const fieldWithType = {}; const propsDb = {}; const relationArrayProps = {}; const relationPopsName = {}; const primaryColumnTypeForRel = {}; for (const [name, type] of nestjs_shared_1.ObjectTyped.entries(propsType)) { Reflect.set(propsDb, name, { type: type, isArray: type === 'array', isNullable: propsNullable.includes(name), }); if (name === primaryColumnName) continue; Reflect.set(fieldWithType, name, type); } for (const [name, value] of nestjs_shared_1.ObjectTyped.entries(relationProperty)) { const relEntityMap = getEntityMap(entityMapProps, value.entityClass); Reflect.set(relationArrayProps, name, value.isArray); Reflect.set(relationPopsName, name, relEntityMap.className); Reflect.set(primaryColumnTypeForRel, name, relEntityMap.primaryColumnType); } return { primaryColumnType, typeName: typeName, fieldWithType, propsDb: propsDb, primaryColumnName: primaryColumnName, relationArrayProps, relationPopsName, primaryColumnTypeForRel, }; } function ZodInputQuerySchema(entity) { return { provide: constants_1.ZOD_INPUT_QUERY_SCHEMA, inject: [ { token: constants_1.ENTITY_MAP_PROPS, optional: false, }, ], useFactory: (entityMapProps) => { const entityMap = getEntityMap(entityMapProps, entity); const { props, relations, relationProperty } = entityMap; const entityRelationStructure = nestjs_shared_1.ObjectTyped.entries(relationProperty).reduce((acum, [name, value]) => { const relMap = getEntityMap(entityMapProps, value.entityClass); Reflect.set(acum, name, relMap.props); return acum; }, {}); return (0, zod_1.zodInputQuery)({ field: props, relations: relations, }, entityRelationStructure); }, }; } function ZodQuerySchema(entity) { return { provide: constants_1.ZOD_QUERY_SCHEMA, inject: [ { token: constants_1.ENTITY_MAP_PROPS, optional: false, }, ], useFactory: (entityMapProps) => { const entityMap = getEntityMap(entityMapProps, entity); const { props, relations, relationProperty, propsType: propsTypeEntity, } = entityMap; const propsType = { ...propsTypeEntity }; const propsArray = { target: {} }; for (const [name, type] of nestjs_shared_1.ObjectTyped.entries(propsTypeEntity)) { if (type !== 'array') continue; Reflect.set(propsArray.target, name, true); } const entityRelationStructure = {}; for (const [name, value] of nestjs_shared_1.ObjectTyped.entries(relationProperty)) { const relMap = getEntityMap(entityMapProps, value.entityClass); if (!(name in propsArray)) { Reflect.set(propsArray, name, {}); } for (const [relNameField, type] of nestjs_shared_1.ObjectTyped.entries(relMap.propsType)) { if (type !== 'array') continue; const propsArrayObject = Reflect.get(propsArray, name); Reflect.set(propsArrayObject, relNameField, true); } Reflect.set(propsType, name, relMap.propsType); Reflect.set(entityRelationStructure, name, relMap.props); } const entityFieldsStructure = { field: props, relations: relations, }; return (0, zod_1.zodQuery)(entityFieldsStructure, entityRelationStructure, propsArray, propsType); }, }; } function ZodPostSchema(entity) { return { provide: constants_1.ZOD_POST_SCHEMA, inject: [ { token: constants_1.ENTITY_MAP_PROPS, optional: false, }, ], useFactory: (entityMapProps) => { const { primaryColumnType, typeName, fieldWithType, propsDb, primaryColumnName, relationArrayProps, relationPopsName, primaryColumnTypeForRel, } = getParamsForOatchANdPostZod(entityMapProps, entity); return (0, zod_1.zodPost)(primaryColumnType, typeName, fieldWithType, propsDb, primaryColumnName, relationArrayProps, relationPopsName, primaryColumnTypeForRel); }, }; } function ZodPatchSchema(entity) { return { provide: constants_1.ZOD_PATCH_SCHEMA, inject: [ { token: constants_1.ENTITY_MAP_PROPS, optional: false, }, ], useFactory: (entityMapProps) => { const { primaryColumnType, typeName, fieldWithType, propsDb, primaryColumnName, relationArrayProps, relationPopsName, primaryColumnTypeForRel, } = getParamsForOatchANdPostZod(entityMapProps, entity); return (0, zod_1.zodPatch)(primaryColumnType, typeName, fieldWithType, propsDb, primaryColumnName, relationArrayProps, relationPopsName, primaryColumnTypeForRel); }, }; } exports.ZodInputPostRelationshipSchema = { provide: constants_1.ZOD_POST_RELATIONSHIP_SCHEMA, useValue: zod_1.zodPostRelationship, }; exports.ZodInputPatchRelationshipSchema = { provide: constants_1.ZOD_PATCH_RELATIONSHIP_SCHEMA, useValue: zod_1.zodPatchRelationship, }; //# sourceMappingURL=zod-validate.factory.js.map