Skip to content

Commit 57d022c

Browse files
committed
fix: Change the actions setting method to v.pipe()
1 parent 68204e8 commit 57d022c

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/valibot/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,45 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
121121
if (isListType(parentType))
122122
return `v.nullable(${gen})`;
123123

124-
let appliedDirectivesGen = applyDirectives(config, field, gen);
125-
126124
if (field.kind === Kind.INPUT_VALUE_DEFINITION) {
127125
const { defaultValue } = field;
128126
if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN)
129-
appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, ${defaultValue.value})`;
127+
gen = `v.optional(${gen}, ${defaultValue.value})`;
130128

131129
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
132-
appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, "${defaultValue.value}")`;
133-
130+
gen = `v.optional(${gen}, "${defaultValue.value}")`;
134131
}
132+
133+
const actions = actionsFromDirectives(config, field);
134+
135135
if (isNonNullType(parentType)) {
136-
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
137-
return "v.string([v.minLength(1)])"; // TODO
136+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
137+
actions.push('v.minLength(1)');
138+
}
138139

139-
return appliedDirectivesGen;
140+
return pipeSchemaAndActions(gen, actions);
140141
}
141142

142-
return `v.nullish(${appliedDirectivesGen})`;
143+
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
143144
}
144145
console.warn('unhandled type:', type);
145146
return '';
146147
}
147148

148-
function applyDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode, gen: string): string {
149+
function actionsFromDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode): string[] {
149150
if (config.directives && field.directives) {
150151
const formatted = formatDirectiveConfig(config.directives);
151-
return `v.pipe(${gen}, ${buildApiForValibot(formatted, field.directives).join(', ')})`;
152+
return buildApiForValibot(formatted, field.directives);
152153
}
153-
return gen;
154+
155+
return [];
156+
}
157+
158+
function pipeSchemaAndActions(schema: string, actions: string[]): string {
159+
if (actions.length === 0)
160+
return schema;
161+
162+
return `v.pipe(${schema}, ${actions.join(', ')})`;
154163
}
155164

156165
function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string {

tests/valibot.spec.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ describe('valibot', () => {
236236
237237
export function PrimitiveInputSchema() {
238238
return v.object({
239-
a: v.string([v.minLength(1)]),
240-
b: v.string([v.minLength(1)]),
239+
a: v.pipe(v.string(), v.minLength(1)),
240+
b: v.pipe(v.string(), v.minLength(1)),
241241
c: v.boolean(),
242242
d: v.number(),
243243
e: v.number()
@@ -280,7 +280,7 @@ describe('valibot', () => {
280280
281281
export function InputNestedSchema() {
282282
return v.object({
283-
field: v.string([v.minLength(1)])
283+
field: v.pipe(v.string(), v.minLength(1))
284284
})
285285
}
286286
"
@@ -439,4 +439,42 @@ describe('valibot', () => {
439439

440440
it.todo('list field');
441441
})
442+
443+
describe('pR #112', () => {
444+
it('with notAllowEmptyString', async () => {
445+
const schema = buildSchema(/* GraphQL */ `
446+
input UserCreateInput {
447+
profile: String! @constraint(maxLength: 5000)
448+
age: Int!
449+
}
450+
451+
directive @constraint(maxLength: Int!) on INPUT_FIELD_DEFINITION
452+
`);
453+
const result = await plugin(
454+
schema,
455+
[],
456+
{
457+
schema: 'valibot',
458+
notAllowEmptyString: true,
459+
directives: {
460+
constraint: {
461+
maxLength: ['maxLength', '$1', 'Please input less than $1'],
462+
},
463+
},
464+
},
465+
{},
466+
);
467+
expect(result.content).toMatchInlineSnapshot(`
468+
"
469+
470+
export function UserCreateInputSchema() {
471+
return v.object({
472+
profile: v.pipe(v.string(), v.maxLength(5000, "Please input less than 5000"), v.minLength(1)),
473+
age: v.number()
474+
})
475+
}
476+
"
477+
`)
478+
});
479+
});
442480
})

0 commit comments

Comments
 (0)