Skip to content

Commit 48d64fa

Browse files
committed
feat: support camelcase
1 parent 580b91a commit 48d64fa

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/valibot/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import {
1818
isNonNullType,
1919
} from './../graphql';
2020

21-
const anySchema = `definedNonNullAnySchema`;
22-
2321
export class ValibotSchemaVisitor extends BaseSchemaVisitor {
2422
constructor(schema: GraphQLSchema, config: ValidationSchemaPluginConfig) {
2523
super(schema, config);
@@ -127,11 +125,12 @@ function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, vis
127125
switch (converter?.targetKind) {
128126
case 'EnumTypeDefinition':
129127
return `${converter.convertName()}Schema`;
128+
case 'ScalarTypeDefinition':
129+
return valibot4Scalar(config, visitor, node.value);
130130
case 'InterfaceTypeDefinition':
131131
case 'InputObjectTypeDefinition':
132132
case 'ObjectTypeDefinition':
133133
case 'UnionTypeDefinition':
134-
case 'ScalarTypeDefinition':
135134
throw new Error('not implemented');
136135
default:
137136
if (converter?.targetKind)
@@ -156,5 +155,5 @@ function valibot4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor,
156155
return `v.boolean()`;
157156
}
158157
console.warn('unhandled scalar name:', scalarName);
159-
return anySchema;
158+
return 'v.any()';
160159
}

tests/valibot.spec.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('valibot', () => {
105105
`);
106106
const scalars = undefined
107107
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
108-
expect(result.content).toMatchInlineSnapshot(`
108+
expect(result.content).toMatchInlineSnapshot(`
109109
"
110110
export const PageTypeSchema = v.enum_(PageType);
111111
@@ -117,4 +117,34 @@ describe('valibot', () => {
117117
"
118118
`);
119119
})
120-
})
120+
121+
it('camelcase', async () => {
122+
const schema = buildSchema(/* GraphQL */ `
123+
input HTTPInput {
124+
method: HTTPMethod
125+
url: URL!
126+
}
127+
128+
enum HTTPMethod {
129+
GET
130+
POST
131+
}
132+
133+
scalar URL # unknown scalar, should be any
134+
`);
135+
const scalars = undefined
136+
const result = await plugin(schema, [], { schema: 'valibot', scalars }, {});
137+
expect(result.content).toMatchInlineSnapshot(`
138+
"
139+
export const HttpMethodSchema = v.enum_(HttpMethod);
140+
141+
export function HttpInputSchema() {
142+
return v.object({
143+
method: v.nullish(HttpMethodSchema),
144+
url: v.any()
145+
})
146+
}
147+
"
148+
`);
149+
})
150+
})

0 commit comments

Comments
 (0)