Skip to content

added notAllowEmptyString option in config #13

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 3 commits into from
Feb 5, 2022
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ type: `boolean` default: `false`

Generates enum as TypeScript `type` instead of `enum`.

### `notAllowEmptyString`

type: `boolean` default: `false`

Generates validation string schema as do not allow empty characters by default.

### `directives`

type: `DirectiveConfig`
Expand Down
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
* ```
*/
enumsAsTypes?: boolean;
/**
* @description Generates validation string schema as do not allow empty characters by default.
* @default false
*
* @exampleMarkdown
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - graphql-codegen-validation-schema
* config:
* notAllowEmptyString: true
* ```
*/
notAllowEmptyString?: boolean;
/**
* @description Generates validation schema with more API based on directive schema.
* @exampleMarkdown
Expand Down
20 changes: 19 additions & 1 deletion src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ const generateInputObjectFieldTypeYupSchema = (
}
if (isNonNullType(type)) {
const gen = generateInputObjectFieldTypeYupSchema(config, tsVisitor, schema, type.type, type);
return maybeLazy(type.type, `${gen}.defined()`);
const nonNullGen = maybeNonEmptyString(config, tsVisitor, gen, type.type);
return maybeLazy(type.type, nonNullGen);
}
if (isNamedType(type)) {
return generateNameNodeYupSchema(config, tsVisitor, schema, type.name);
Expand Down Expand Up @@ -162,6 +163,23 @@ const maybeLazy = (type: TypeNode, schema: string): string => {
return schema;
};

const maybeNonEmptyString = (
config: ValidationSchemaPluginConfig,
tsVisitor: TsVisitor,
schema: string,
childType: TypeNode
): string => {
if (config.notAllowEmptyString === true && isNamedType(childType)) {
const maybeScalarName = childType.name.value;
const tsType = tsVisitor.scalars[maybeScalarName];
if (tsType === 'string') {
return `${schema}.required()`;
}
}
// fallback
return `${schema}.defined()`;
};

const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
const tsType = tsVisitor.scalars[scalarName];
switch (tsType) {
Expand Down
4 changes: 4 additions & 0 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ const generateInputObjectFieldTypeZodSchema = (
if (isNamedType(type)) {
const gen = generateNameNodeZodSchema(tsVisitor, schema, type.name);
if (isNonNullType(parentType)) {
if (config.notAllowEmptyString === true) {
const tsType = tsVisitor.scalars[type.name.value];
if (tsType === 'string') return `${gen}.min(1)`;
}
return gen;
}
if (isListType(parentType)) {
Expand Down
31 changes: 31 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,35 @@ describe('yup', () => {
);
expect(result.content).toContain("export const PageTypeSchema = yup.mixed().oneOf(['PUBLIC', 'BASIC_AUTH'])");
});

it('with notAllowEmptyString', async () => {
const schema = buildSchema(/* GraphQL */ `
input PrimitiveInput {
a: ID!
b: String!
c: Boolean!
d: Int!
e: Float!
}
`);
const result = await plugin(
schema,
[],
{
notAllowEmptyString: true,
},
{}
);
const wantContains = [
'export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>',
'a: yup.string().required(),',
'b: yup.string().required(),',
'c: yup.boolean().defined(),',
'd: yup.number().defined(),',
'e: yup.number().defined()',
];
for (const wantContain of wantContains) {
expect(result.content).toContain(wantContain);
}
});
});
32 changes: 32 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,36 @@ describe('zod', () => {
);
expect(result.content).toContain("export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH'])");
});

it('with notAllowEmptyString', async () => {
const schema = buildSchema(/* GraphQL */ `
input PrimitiveInput {
a: ID!
b: String!
c: Boolean!
d: Int!
e: Float!
}
`);
const result = await plugin(
schema,
[],
{
schema: 'zod',
notAllowEmptyString: true,
},
{}
);
const wantContains = [
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
'a: z.string().min(1),',
'b: z.string().min(1),',
'c: z.boolean(),',
'd: z.number(),',
'e: z.number()',
];
for (const wantContain of wantContains) {
expect(result.content).toContain(wantContain);
}
});
});