Skip to content

fix #386 #391

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 2 commits into from
Jun 9, 2023
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
5 changes: 2 additions & 3 deletions src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ const generateFieldTypeMyZodSchema = (
}
const appliedDirectivesGen = applyDirectives(config, field, gen);
if (isNonNullType(parentType)) {
if (config.notAllowEmptyString === true) {
const tsType = visitor.getScalarType(type.name.value);
if (tsType === 'string') return `${gen}.min(1)`;
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
return `${gen}.min(1)`;
}
return appliedDirectivesGen;
}
Expand Down
22 changes: 19 additions & 3 deletions src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { TsVisitor } from '@graphql-codegen/typescript';
import { GraphQLSchema, NameNode } from 'graphql';
import { GraphQLSchema, NameNode, specifiedScalarTypes } from 'graphql';

import { ValidationSchemaPluginConfig } from './config';

export class Visitor extends TsVisitor {
constructor(
private scalarDirection: 'input' | 'output' | 'both',
private schema: GraphQLSchema,
config: ValidationSchemaPluginConfig
private pluginConfig: ValidationSchemaPluginConfig
) {
super(schema, config);
super(schema, pluginConfig);
}

private isSpecifiedScalarName(scalarName: string) {
return specifiedScalarTypes.some(({ name }) => name === scalarName);
}

public getType(name: string) {
Expand All @@ -34,4 +38,16 @@ export class Visitor extends TsVisitor {
}
return this.scalars[scalarName][this.scalarDirection];
}

public shouldEmitAsNotAllowEmptyString(name: string): boolean {
if (this.pluginConfig.notAllowEmptyString !== true) {
return false;
}
const typ = this.getType(name);
if (typ?.astNode?.kind !== 'ScalarTypeDefinition' && !this.isSpecifiedScalarName(name)) {
return false;
}
const tsType = this.getScalarType(name);
return tsType === 'string';
}
}
5 changes: 2 additions & 3 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,8 @@ const generateFieldTypeYupSchema = (
if (isNamedType(type)) {
const gen = generateNameNodeYupSchema(config, visitor, type.name);
if (isNonNullType(parentType)) {
if (config.notAllowEmptyString === true) {
const tsType = visitor.getScalarType(type.name.value);
if (tsType === 'string') return `${gen}.required()`;
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
return `${gen}.required()`;
}
return `${gen}.nonNullable()`;
}
Expand Down
5 changes: 2 additions & 3 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,8 @@ const generateFieldTypeZodSchema = (
}
const appliedDirectivesGen = applyDirectives(config, field, gen);
if (isNonNullType(parentType)) {
if (config.notAllowEmptyString === true) {
const tsType = visitor.getScalarType(type.name.value);
if (tsType === 'string') return `${appliedDirectivesGen}.min(1)`;
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
return `${appliedDirectivesGen}.min(1)`;
}
return appliedDirectivesGen;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildSchema } from 'graphql';
import dedent from 'ts-dedent';

import { plugin } from '../src/index';

Expand Down Expand Up @@ -297,6 +298,37 @@ describe('myzod', () => {
}
});

it('with notAllowEmptyString issue #386', async () => {
const schema = buildSchema(/* GraphQL */ `
input InputOne {
field: InputNested!
}

input InputNested {
field: String!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'myzod',
notAllowEmptyString: true,
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function InputNestedSchema(): myzod.Type<InputNested> {
return myzod.object({
field: myzod.string().min(1)
})
}`;
expect(result.content).toContain(wantContain);
});

it('with scalarSchemas', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
Expand Down
32 changes: 32 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildSchema } from 'graphql';
import dedent from 'ts-dedent';

import { plugin } from '../src/index';

Expand Down Expand Up @@ -294,6 +295,37 @@ describe('yup', () => {
}
});

it('with notAllowEmptyString issue #386', async () => {
const schema = buildSchema(/* GraphQL */ `
input InputOne {
field: InputNested!
}

input InputNested {
field: String!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'yup',
notAllowEmptyString: true,
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function InputNestedSchema(): yup.ObjectSchema<InputNested> {
return yup.object({
field: yup.string().defined().required()
})
}`;
expect(result.content).toContain(wantContain);
});

it('with scalarSchemas', async () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
Expand Down
32 changes: 32 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildSchema } from 'graphql';
import { dedent } from 'ts-dedent';

import { plugin } from '../src/index';

Expand Down Expand Up @@ -297,6 +298,37 @@ describe('zod', () => {
}
});

it('with notAllowEmptyString issue #386', async () => {
const schema = buildSchema(/* GraphQL */ `
input InputOne {
field: InputNested!
}

input InputNested {
field: String!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
notAllowEmptyString: true,
scalars: {
ID: 'string',
},
},
{}
);
const wantContain = dedent`
export function InputNestedSchema(): z.ZodObject<Properties<InputNested>> {
return z.object({
field: z.string().min(1)
})
}`;
expect(result.content).toContain(wantContain);
});

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