From 9b0eb1927345d15e16888ccfd2dc218d4254aa00 Mon Sep 17 00:00:00 2001 From: Peramanathan Sathyamoorthy Date: Fri, 17 Jan 2025 21:08:59 +0100 Subject: [PATCH] Update readme --- README.md | 86 +++++++++++++++++++++---------------------------------- 1 file changed, 32 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index f477ec6..3df9129 100644 --- a/README.md +++ b/README.md @@ -133,18 +133,21 @@ updatedSchema.parse({ ## What more can be cooked? +#### Conditional Schema Transformer +
-Apply Conditional Requirements (needs improvement) +Make Conditional Schema Transformer -You can apply conditional requirements to a Zod schema using the applyConditionalRequirements function. (Didn't work in improving it, generated by ChatGPT as is) +You can make conditional schema transfer using data early and later use the transformer taking `schema` and `config` with conditionals (`requiredIf`). ```ts import { z } from 'zod'; -import { applyConditionalRequirements } from 'adaptate'; +import { makeConditionalSchemaTransformer } from 'adaptate'; const schema = z.object({ firstName: z.string().optional(), secondName: z.string().optional(), + parentContactNumber: z.number().optional(), age: z.number().optional(), address: z .object({ @@ -156,18 +159,36 @@ const schema = z.object({ }); const config = { - age: true, - // explicit - firstName: { - requiredIf: (data: any) => data.age > 18, + parentContactNumber: { + requiredIf: (data: any) => data.age < 18, }, - // or implicit - secondName: (data) => !!data.firstName, + age: true, + secondName: (data: any) => !!data.firstName, }; -const data = { age: 20 }; +let firstNameRequiredData = { + firstName: 'Mario', + age: 17, +}; + +let conditionalTransformer = makeConditionalSchemaTransformer( + firstNameRequiredData +); + +let transformer = conditionalTransformer(schema, config); + +let transformedSchema = transformer.schema; + +// To parse the data and validate +transformer.run(); // will throw as parentContactNumber is required +// Equivalent to transformer.schema.parse(data); and reduces verbosity -const updatedSchema = applyConditionalRequirements(schema, config, data); +// Removes the conditional requirement if you want to use it with +// Regular transformSchema function +transformer.staticConfig = { + age: true, + secondName: (data: any) => !!data.firstName, +}; ```
@@ -289,49 +310,6 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT } ``` -### Make Conditional Schema Transformer - -You can make conditional schema transfer using data early and later use the transformer taking `schema` and `config` with conditionals (`requiredIf`). - -```ts -import { z } from 'zod'; -import { makeConditionalSchemaTransformer } from 'adaptate'; - -const schema = z.object({ - firstName: z.string().optional(), - secondName: z.string().optional(), - age: z.number().optional(), - address: z - .object({ - street: z.string().optional(), - city: z.string().optional(), - }) - .optional(), - title: z.string().optional(), -}); - -const config = { - firstName: { - requiredIf: (data: any) => data.age > 18, - }, - age: true, - secondName: (data: any) => !!data.firstName, -}; - -let firstNameRequiredData = { - firstName: 'John', - age: 20, -}; -let secondNameRequiredData = { - firstName: 'Peram', -}; - -makeConditionalSchemaTransformer({ - ...firstNameRequiredData, - age: 10, -})(schema, config).run(); -``` - ### Converting OpenAPI Schema to Zod Schema (most commonly needed) Refer [@adaptate/utils README](/packages/utils/README.md#converting-openapi-schema-to-zod-schema-most-commonly-needed)