Skip to content

feat: allows for passthrough of a default/fallback scalar type #847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ config:
Email: z.string().email()
```

### `defaultScalarTypeSchema`

type: `string`

Fallback scalar type for undefined scalar types in the schema not found in `scalarSchemas`.

#### yup schema
```yml
config:
schema: yup
defaultScalarSchema: yup.unknown()
```

#### zod schema
```yml
config:
schema: zod
defaultScalarSchema: z.unknown()
```

### `withObjectType`

type: `boolean` default: `false`
Expand Down
18 changes: 18 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
* ```
*/
scalarSchemas?: ScalarSchemas
/**
* @description Fallback scalar type for undefined scalar types in the schema not found in `scalarSchemas`.
*
* @exampleMarkdown
* ```yml
* config:
* schema: yup
* defaultScalarSchema: yup.unknown()
* ```
*
* @exampleMarkdown
* ```yml
* config:
* schema: zod
* defaultScalarSchema: z.unknown()
* ```
*/
defaultScalarTypeSchema?: string
/**
* @description Generates validation schema with GraphQL type objects.
* but excludes "Query", "Mutation", "Subscription" objects.
Expand Down
5 changes: 5 additions & 0 deletions src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ function myzod4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, sc
case 'boolean':
return `myzod.boolean()`;
}

if (config.defaultScalarTypeSchema) {
return config.defaultScalarTypeSchema;
}

console.warn('unhandled name:', scalarName);
return anySchema;
}
5 changes: 5 additions & 0 deletions src/valibot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ function valibot4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor,
case 'boolean':
return `v.boolean()`;
}

if (config.defaultScalarTypeSchema) {
return config.defaultScalarTypeSchema;
}

console.warn('unhandled scalar name:', scalarName);
return 'v.any()';
}
5 changes: 5 additions & 0 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ function yup4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scal
case 'boolean':
return `yup.boolean().defined()`;
}

if (config.defaultScalarTypeSchema) {
return config.defaultScalarTypeSchema
}

console.warn('unhandled name:', scalarName);
return `yup.mixed()`;
}
5 changes: 5 additions & 0 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ function zod4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scal
case 'boolean':
return `z.boolean()`;
}

if (config.defaultScalarTypeSchema) {
return config.defaultScalarTypeSchema;
}

console.warn('unhandled scalar name:', scalarName);
return anySchema;
}
35 changes: 35 additions & 0 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,41 @@ describe('myzod', () => {
"
`)
});

it('with defaultScalarTypeSchema', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
date: Date!
email: Email
str: String!
}
scalar Date
scalar Email
`);
const result = await plugin(
schema,
[],
{
schema: 'myzod',
scalarSchemas: {
Email: 'myzod.string()', // generate the basic type. User can later extend it using `withPredicate(fn: (val: string) => boolean), errMsg?: string }`
},
defaultScalarTypeSchema: 'myzod.string()',
},
{},
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export function ScalarsInputSchema(): myzod.Type<ScalarsInput> {
return myzod.object({
date: myzod.string(),
email: myzod.string().optional().nullable(),
str: myzod.string()
})
}
"
`)
});
it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
Expand Down
37 changes: 37 additions & 0 deletions tests/valibot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,43 @@ describe('valibot', () => {
"
`)
});

it('with defaultScalarTypeSchema', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
date: Date!
email: Email
str: String!
}
scalar Date
scalar Email
`);
const result = await plugin(
schema,
[],
{
schema: 'valibot',
scalarSchemas: {
Email: 'v.string([v.email()])',
},
defaultScalarTypeSchema: 'v.string()',
},
{},
);
expect(result.content).toMatchInlineSnapshot(`
"

export function ScalarsInputSchema(): v.GenericSchema<ScalarsInput> {
return v.object({
date: v.string(),
email: v.nullish(v.string([v.email()])),
str: v.string()
})
}
"
`)
});

it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
Expand Down
35 changes: 35 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,41 @@ describe('yup', () => {
`)
});

it('with defaultScalarTypeSchema', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
date: Date!
email: Email
str: String!
}
scalar Date
scalar Email
`);
const result = await plugin(
schema,
[],
{
scalarSchemas: {
Email: 'yup.string().email()',
},
defaultScalarTypeSchema: 'yup.string()',
},
{},
);
expect(result.content).toMatchInlineSnapshot(`
"

export function ScalarsInputSchema(): yup.ObjectSchema<ScalarsInput> {
return yup.object({
date: yup.string().nonNullable(),
email: yup.string().email().defined().nullable().optional(),
str: yup.string().defined().nonNullable()
})
}
"
`)
});

it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
Expand Down
35 changes: 35 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,41 @@ describe('zod', () => {
`)
});

it('with defaultScalarTypeSchema', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
date: Date!
email: Email
str: String!
}
scalar Date
scalar Email
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
scalarSchemas: {
Email: 'z.string().email()',
},
defaultScalarTypeSchema: 'z.string()',
},
{},
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export function ScalarsInputSchema(): z.ZodObject<Properties<ScalarsInput>> {
return z.object({
date: z.string(),
email: z.string().email().nullish(),
str: z.string()
})
}
"
`)
});

it('with typesPrefix', async () => {
const schema = buildSchema(/* GraphQL */ `
input Say {
Expand Down
Loading