graphql-joi-directives
Add constraint validation to your GraphQL Inputs using Joi
Install
Add the package using npm i @ephys/graphql-joi-directives
This package was built with graphql-tools and supports Apollo.
Add the directives and the type definitions to your GraphQL schema:
import { joiConstraintDirectives, joiContraintDirectivesTypedefs } from '@ephys/graphql-joi-directives';
const schema = makeExecutableSchema({
typeDefs: [...joiContraintDirectivesTypedefs],
schemaDirectives: {
...joiConstraintDirectives,
},
});
const server = new ApolloServer({ schema });
The directives
By default, the following directives are exposed.
They can be used on fields in inputs & on arguments definitions
Strings: @str
Can be used on the String
& [String]
types
directive @str(
min: Int,
max: Int,
length: Int,
trim: Boolean,
pattern: String,
creditCard: Boolean,
case: JoiDirectiveCaseEnum
isoDate: Boolean,
isoDuration: Boolean,
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
enum JoiDirectiveCaseEnum {
UPPER
LOWER
}
Ints: @int
Can be used on the Int
& [Int]
types
directive @int(
min: Int,
max: Int
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
Floats: @float
Can be used on the Float
& [Float]
types
directive @float(
min: Float,
max: Float,
minExclusive: Float,
maxExclusive: Float,
precision: Int,
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
Lists: @list
Can be used on any list type
directive @list(
min: Int,
max: Int,
length: Int,
unique: Boolean,
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION