Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
p10ns11y committed Jan 17, 2025
1 parent d34aad6 commit 9b0eb19
Showing 1 changed file with 32 additions and 54 deletions.
86 changes: 32 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,21 @@ updatedSchema.parse({

## What more can be cooked?

#### Conditional Schema Transformer

<details>
<summary>Apply Conditional Requirements (needs improvement)</summary>
<summary>Make Conditional Schema Transformer</summary>

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({
Expand All @@ -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,
};
```

</details>
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9b0eb19

Please sign in to comment.