diff --git a/src/lesan.ts b/src/lesan.ts index c86a255..d62524a 100644 --- a/src/lesan.ts +++ b/src/lesan.ts @@ -18,6 +18,6 @@ export const lesan = () => { odm: { ...odm(schemasObj) }, runServer: lesanServer(schemasObj, actsObj), contextFns, - generateSchemTypes: () => generateSchemTypes(schemasObj), + generateSchemTypes: () => generateSchemTypes(schemasObj, actsObj), }; }; diff --git a/src/models/selectStruct.ts b/src/models/selectStruct.ts index c11646e..22f3394 100644 --- a/src/models/selectStruct.ts +++ b/src/models/selectStruct.ts @@ -35,7 +35,7 @@ export const selectStructFns = (schemasObj: TSchemas) => { }; } - const mainRelations = ( + const numDepthIterateRelations = ( schema: keyof TSchemas, depth: number, pureObj: Record, @@ -45,30 +45,30 @@ export const selectStructFns = (schemasObj: TSchemas) => { for (const property in foundedSchema.mainRelations) { returnObj = { ...returnObj, - [property]: selectStruct( + [property]: optional(selectStruct( foundedSchema.mainRelations[property].schemaName, depth, - ), + )), }; } for (const property in foundedSchema.relatedRelations) { returnObj = { ...returnObj, - [property]: selectStruct( + [property]: optional(selectStruct( foundedSchema.relatedRelations[property].schemaName, depth, - ), + )), }; } - return optional(object(returnObj as ObjectSchema)); + return object(returnObj as ObjectSchema); }; const numberDepth = (depth: number, pureObj: Record) => { depth--; return depth > -1 - ? mainRelations(schema, depth, pureObj) - : optional(object(pureObj)); + ? numDepthIterateRelations(schema, depth, pureObj) + : object(pureObj); }; const objectDepth = (depth: any, pureObj: Record) => { @@ -79,24 +79,24 @@ export const selectStructFns = (schemasObj: TSchemas) => { checkRelation(depth, property) && (pureObj = { ...pureObj, - [property]: selectStruct( + [property]: optional(selectStruct( foundedSchema.mainRelations[property].schemaName, depth[property], - ), + )), }); } for (const property in foundedSchema.relatedRelations) { checkRelation(depth, property) && (pureObj = { ...pureObj, - [property]: selectStruct( + [property]: optional(selectStruct( foundedSchema.relatedRelations[property].schemaName, depth[property], - ), + )), }); } - return optional(object(pureObj)); + return object(pureObj); }; const completeObj = typeof depth === "number" diff --git a/src/npmDeps.ts b/src/npmDeps.ts index 984c07f..4209ca1 100644 --- a/src/npmDeps.ts +++ b/src/npmDeps.ts @@ -1,2 +1,2 @@ -export * from "npm:superstruct@1.0.3"; +export * from "npm:superstruct@2.0.2"; export * from "npm:mongodb@6.3.0"; diff --git a/src/types/generateTypesFromStruct.ts b/src/types/generateTypesFromStruct.ts index 5eaabe3..e9020a2 100644 --- a/src/types/generateTypesFromStruct.ts +++ b/src/types/generateTypesFromStruct.ts @@ -8,6 +8,15 @@ export const generateTypesFromStruct = ( }, ) => { let returnStr = ""; + + const schemaStructValidate = schemaStruct.validate(undefined)[0]; + + if (schemaStructValidate === undefined || schemaStructValidate === null) { + if (keyname && !keyname.endsWith("?")) { + keyname = `${keyname}?`; + } + } + if (schemaStruct.type === "object") { returnStr = returnStr + (keyname ? `${keyname}: {\n` : `{\n`); for (const key in schemaStruct.schema as Object) {