Validation
Introduction
Inputs validation is an important piece in every backend application but is usually a source of trouble and result in a lot of boilerplate and duplicated code.
With @foadonis/graphql it is possible to have your validation schema as close as possible as your GraphQL schema (inputs, args, etc).
Disclaimer
As of today, VineJS is not supported out of the box and validation libraries that use decorators (class-validator, joiful) are not maintained anymore and you might want to consider manual validation for now.
class-validator
class-validator is a validation library relying on decorators, allowing you to define your GraphQL schema and validation schema the same way.
npm install class-validatorexport default defineConfig({
// [...]
validate: true,
})We can now define our CreateRecipeInput schema with the following:
@InputType()
export class CreateRecipeInput {
@Field()
@Length(3, 30)
declare title: string
@Field({ nullable: true })
@Length(30, 255)
declare description?: string
}And that's it! When this input is used in a resolver the data will be automatically validated:
@Resolver(() => Recipe)
class RecipeResolver {
@Mutation(() => Recipe)
createRecipe(@Arg('input') data: CreateRecipeInput) {
return Recipe.create(data)
}
}Custom validator
WIP