1.0.4 • Published 4 years ago

graphql-codegen-yup-schema v1.0.4

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

graphql-codegen-yup-schema

graphql-codegen plugin to generate yup validation schema based on GraphQL schema directives

Install

install using yarn:

$ yarn add -D graphql-codegen-yup-schema

Prerequisite

You need to specify the following directive definition in your GraphQL schema.

directive @constraint(
  minLength: Int
  maxLength: Int
  pattern: String
  min: Int
  max: Int
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

What it does for you

let's say you have a following mutation with constraint directive.

input RegisterAddressInput {
  postalCode: String @constraint(minLength: 7, maxLength: 7)
  state: String @constraint(maxLength: 4)
  city: String @constraint(maxLength: 32)
  line1: String @constraint(maxLength: 32)
  line2: String @constraint(maxLength: 32)
}

then use this plugin with graphql-codegen with the config like below

schema: ./graphql/generated/schema.graphql
documents:
  - ./graphql/mutations/*.gql
generates:
  ./graphql/generated/validationSchemas.ts:
    - graphql-codegen-yup-schema

it will generate a yup validation object💪

import * as yup from 'yup';
export const RegisterAddressInputValidationSchema = yup.object().shape({
  postalCode: yup
    .string()
    .min(7)
    .max(7),
  state: yup.string().max(4),
  city: yup.string().max(32),
  line1: yup.string().max(32),
  line2: yup.string().max(32)
});

Supported constraints

nametypedescription
minLengthintmin length of a string
maxLengthintmax length of a string
patternstringregex for a string
minintmin value of a number
maxintmax value of a number
1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago