Skip to content

Commit

Permalink
Allow makeConditionalSchemaTransformer use normal config
Browse files Browse the repository at this point in the history
  • Loading branch information
p10ns11y committed Dec 5, 2024
1 parent 2191228 commit f283732
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
4 changes: 2 additions & 2 deletions coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -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}}
}
138 changes: 136 additions & 2 deletions packages/core/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ describe('makeConditionalSchemaTransformer', () => {
firstName: {
requiredIf: (data: any) => data.age > 18,
},
age: true,
secondName: (data: any) => !!data.firstName,
};

Expand All @@ -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();
Expand All @@ -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(),
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export function makeConditionalSchemaTransformer(data: any) {
let transformer = {
run: () => schema.parse(data),
schema: schema,
staticConfig: {},
};
if (
schema instanceof ZodObject &&
Expand All @@ -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;
Expand Down

0 comments on commit f283732

Please sign in to comment.