From f283732959287d4071766c372d56b3c9a96c2873 Mon Sep 17 00:00:00 2001 From: Peramanathan Sathyamoorthy Date: Thu, 5 Dec 2024 17:49:20 +0100 Subject: [PATCH] Allow makeConditionalSchemaTransformer use normal config --- coverage/coverage-summary.json | 4 +- packages/core/src/__tests__/index.test.ts | 138 +++++++++++++++++++++- packages/core/src/index.ts | 9 +- 3 files changed, 146 insertions(+), 5 deletions(-) diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json index 058e34b..87eb336 100644 --- a/coverage/coverage-summary.json +++ b/coverage/coverage-summary.json @@ -1,5 +1,5 @@ -{"total": {"lines":{"total":206,"covered":206,"skipped":0,"pct":100},"statements":{"total":206,"covered":206,"skipped":0,"pct":100},"functions":{"total":11,"covered":10,"skipped":0,"pct":90.9},"branches":{"total":87,"covered":80,"skipped":0,"pct":91.95},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} -,"/Users/peram/code/adaptate/packages/core/src/index.ts": {"lines":{"total":91,"covered":91,"skipped":0,"pct":100},"functions":{"total":6,"covered":5,"skipped":0,"pct":83.33},"statements":{"total":91,"covered":91,"skipped":0,"pct":100},"branches":{"total":39,"covered":39,"skipped":0,"pct":100}} +{"total": {"lines":{"total":212,"covered":212,"skipped":0,"pct":100},"statements":{"total":212,"covered":212,"skipped":0,"pct":100},"functions":{"total":11,"covered":10,"skipped":0,"pct":90.9},"branches":{"total":89,"covered":82,"skipped":0,"pct":92.13},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} +,"/Users/peram/code/adaptate/packages/core/src/index.ts": {"lines":{"total":97,"covered":97,"skipped":0,"pct":100},"functions":{"total":6,"covered":5,"skipped":0,"pct":83.33},"statements":{"total":97,"covered":97,"skipped":0,"pct":100},"branches":{"total":41,"covered":41,"skipped":0,"pct":100}} ,"/Users/peram/code/adaptate/packages/utils/src/load-yaml.ts": {"lines":{"total":14,"covered":14,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":14,"covered":14,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}} ,"/Users/peram/code/adaptate/packages/utils/src/openapi.ts": {"lines":{"total":101,"covered":101,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":101,"covered":101,"skipped":0,"pct":100},"branches":{"total":47,"covered":40,"skipped":0,"pct":85.1}} } diff --git a/packages/core/src/__tests__/index.test.ts b/packages/core/src/__tests__/index.test.ts index 358cd46..ed550aa 100644 --- a/packages/core/src/__tests__/index.test.ts +++ b/packages/core/src/__tests__/index.test.ts @@ -398,6 +398,7 @@ describe('makeConditionalSchemaTransformer', () => { firstName: { requiredIf: (data: any) => data.age > 18, }, + age: true, secondName: (data: any) => !!data.firstName, }; @@ -414,17 +415,51 @@ describe('makeConditionalSchemaTransformer', () => { ...firstNameRequiredData, age: 10, })(schema, config).run() - ).toThrow(); + ).toThrowErrorMatchingInlineSnapshot(` + [ZodError: [ + { + "code": "invalid_type", + "expected": "string", + "received": "undefined", + "path": [ + "secondName" + ], + "message": "Required" + } + ]] + `); expect(() => makeConditionalSchemaTransformer(secondNameRequiredData)( schema, config ).run() - ).toThrow(); + ).toThrowErrorMatchingInlineSnapshot(` + [ZodError: [ + { + "code": "invalid_type", + "expected": "string", + "received": "undefined", + "path": [ + "secondName" + ], + "message": "Required" + }, + { + "code": "invalid_type", + "expected": "number", + "received": "undefined", + "path": [ + "age" + ], + "message": "Required" + } + ]] + `); expect(() => makeConditionalSchemaTransformer({ ...secondNameRequiredData, + age: 37, secondName: 'Sathyam', })(schema, config).run() ).not.toThrow(); @@ -440,6 +475,105 @@ describe('makeConditionalSchemaTransformer', () => { expect(result.schema).toBe(schema); }); + it('should returns a result with updatedSchema, runner, staticConfig', () => { + const schema = z.object({ + name: z.string().optional(), + age: z.number().optional(), + canBuyAlcohol: z.boolean().optional(), + }); + + const config = { + name: true, + canBuyAlcohol: { + requiredIf: (data: any) => data.age >= 18, + }, + }; + const data = { + name: 'Peram', + age: 37, + }; + + const result = makeConditionalSchemaTransformer(data)(schema, config); + + expect(result).toMatchInlineSnapshot(` + { + "run": [Function], + "schema": ZodObject { + "_cached": null, + "_def": { + "catchall": ZodNever { + "_def": { + "typeName": "ZodNever", + }, + "and": [Function], + "array": [Function], + "brand": [Function], + "catch": [Function], + "default": [Function], + "describe": [Function], + "isNullable": [Function], + "isOptional": [Function], + "nullable": [Function], + "nullish": [Function], + "optional": [Function], + "or": [Function], + "parse": [Function], + "parseAsync": [Function], + "pipe": [Function], + "promise": [Function], + "readonly": [Function], + "refine": [Function], + "refinement": [Function], + "safeParse": [Function], + "safeParseAsync": [Function], + "spa": [Function], + "superRefine": [Function], + "transform": [Function], + }, + "shape": [Function], + "typeName": "ZodObject", + "unknownKeys": "strip", + }, + "and": [Function], + "array": [Function], + "augment": [Function], + "brand": [Function], + "catch": [Function], + "default": [Function], + "describe": [Function], + "isNullable": [Function], + "isOptional": [Function], + "nonstrict": [Function], + "nullable": [Function], + "nullish": [Function], + "optional": [Function], + "or": [Function], + "parse": [Function], + "parseAsync": [Function], + "pipe": [Function], + "promise": [Function], + "readonly": [Function], + "refine": [Function], + "refinement": [Function], + "safeParse": [Function], + "safeParseAsync": [Function], + "spa": [Function], + "superRefine": [Function], + "transform": [Function], + }, + "staticConfig": { + "name": true, + }, + } + `); + + expect(result.staticConfig).toMatchInlineSnapshot(` + { + "name": true, + } + `); + }); + it('should handle non-object config', () => { const schema = z.object({ name: z.string().optional(), diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 8cf26b0..2729945 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -129,6 +129,7 @@ export function makeConditionalSchemaTransformer(data: any) { let transformer = { run: () => schema.parse(data), schema: schema, + staticConfig: {}, }; if ( schema instanceof ZodObject && @@ -147,12 +148,18 @@ export function makeConditionalSchemaTransformer(data: any) { // @ts-ignore return [key, value.unwrap()]; } + } else if (config[key]) { + // @ts-ignore + transformer.staticConfig[key] = config[key]; } return [key, value]; }) ); - let updatedSchema = z.object(newShape); + let updatedSchema = transformSchema( + z.object(newShape), + transformer.staticConfig + ); transformer.run = () => updatedSchema.parse(data); transformer.schema = updatedSchema;