Skip to content

Commit

Permalink
Improve makeConditionalSchemaTransformer tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
p10ns11y committed Jan 17, 2025
1 parent 0265acd commit 6a96a7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,13 @@ I have attempted to recreate what I have done at work with the help of **ChatGPT
}
```

### 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(),
Expand All @@ -311,18 +311,25 @@ const schema = z.object({
});

const config = {
age: true,
// explicit
firstName: {
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: 'John',
age: 20,
};
let secondNameRequiredData = {
firstName: 'Peram',
};

const updatedSchema = applyConditionalRequirements(schema, config, data);
makeConditionalSchemaTransformer({
...firstNameRequiredData,
age: 10,
})(schema, config).run();
```

### Converting OpenAPI Schema to Zod Schema (most commonly needed)
Expand Down
36 changes: 19 additions & 17 deletions packages/core/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ describe('makeConditionalSchemaTransformer', () => {
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 @@ -395,26 +396,27 @@ describe('makeConditionalSchemaTransformer', () => {
});

const config = {
firstName: {
requiredIf: (data: any) => data.age > 18,
parentContactNumber: {
requiredIf: (data: any) => data.age < 18,
},
age: true,
secondName: (data: any) => !!data.firstName,
};

let firstNameRequiredData = {
firstName: 'John',
age: 20,
firstName: 'Mario',
age: 17,
};
let secondNameRequiredData = {
firstName: 'Peram',
age: 24,
};

expect(() =>
makeConditionalSchemaTransformer({
...firstNameRequiredData,
age: 10,
})(schema, config).run()
makeConditionalSchemaTransformer(firstNameRequiredData)(
schema,
config
).run()
).toThrowErrorMatchingInlineSnapshot(`
[ZodError: [
{
Expand All @@ -425,6 +427,15 @@ describe('makeConditionalSchemaTransformer', () => {
"secondName"
],
"message": "Required"
},
{
"code": "invalid_type",
"expected": "number",
"received": "undefined",
"path": [
"parentContactNumber"
],
"message": "Required"
}
]]
`);
Expand All @@ -444,15 +455,6 @@ describe('makeConditionalSchemaTransformer', () => {
"secondName"
],
"message": "Required"
},
{
"code": "invalid_type",
"expected": "number",
"received": "undefined",
"path": [
"age"
],
"message": "Required"
}
]]
`);
Expand Down

0 comments on commit 6a96a7d

Please sign in to comment.