Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ephys/graphql-joi-directives

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ephys/graphql-joi-directives

Adds Joi-powered constraint directive for GraphQL

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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({
  //            V this is an array
  typeDefs: [...joiContraintDirectivesTypedefs],
  schemaDirectives: {
    // V this is an object
    ...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(
  # requires a minimum length of {min}
  min: Int,
  # requires a maximum length of {max}
  max: Int,
  # requires a length of exactly {length}
  length: Int,
  # trims the string before validation
  trim: Boolean,
  # ensure the string matches the provided regexp.
  # example
  # | pattern: "/[a-zA-Z0-9]/i"
  pattern: String,
  # requires the string to match a credit card format
  creditCard: Boolean,
  # converts casing, can be 'UPPER' or 'LOWER'
  case: JoiDirectiveCaseEnum
  # format must be a valid ISO date
  isoDate: Boolean,
  # format must be a valid ISO duration
  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(
  # value must be greater than or equal to {min}
  min: Float,
  # value must be less than or equal to {max}
  max: Float,
  # value must be greater than {minExclusive}
  # Joi: number.greater - https://joi.dev/api/?v=17.4.0#numbergreaterlimit
  minExclusive: Float,
  # value must be less than {maxExclusive}
  # Joi: number.less - https://joi.dev/api/?v=17.4.0#numberlesslimit
  maxExclusive: Float,
  # sets a maximum number of decimal places allowed
  precision: Int,
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

Lists: @list

Can be used on any list type

directive @list(
  # requires a minimum of {min} items in the list
  min: Int,
  # requires a maximum of {max} items in the list
  max: Int,
  # requires exactly {length} items in the list
  length: Int,
  # removes duplicate items from the list
  unique: Boolean,
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

Keywords

FAQs

Package last updated on 08 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc