npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@envelop/extended-validation

v4.1.0

Published

Extended validation plugin adds support for writing GraphQL validation rules, that has access to all `execute` parameters, including variables.

Downloads

172,197

Readme

@envelop/extended-validation

Extended validation plugin adds support for writing GraphQL validation rules, that has access to all execute parameters, including variables.

While GraphQL supports fair amount of built-in validations, and validations could be extended, it's doesn't expose variables to the validation rules, since operation variables are not available during validate flow (it's only available through execution of the operation, after input/variables coercion is done).

This plugin runs before validate but allow developers to write their validation rules in the same way GraphQL ValidationRule is defined (based on a GraphQL visitor).

Getting Started

Start by installing the plugin:

yarn add @envelop/extended-validation

Then, use the plugin with your validation rules:

import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine } from '@envelop/core'
import { useExtendedValidation } from '@envelop/extended-validation'

const getEnveloped = envelop({
  plugins: [
    useEngine({ parse, validate, specifiedRules, execute, subscribe }),
    useExtendedValidation({
      rules: [
        /* ... your rules here  */
      ]
    })
  ]
})

To create your custom rules, implement the ExtendedValidationRule interface and return your GraphQL AST visitor.

For example:

import { ExtendedValidationRule } from '@envelop/extended-validation'

export const MyRule: ExtendedValidationRule = (validationContext, executionArgs) => {
  return {
    OperationDefinition: node => {
      // This will run for every executed Query/Mutation/Subscription
      // And now you also have access to the execution params like variables, context and so on.
      // If you wish to report an error, use validationContext.reportError or throw an exception.
    }
  }
}

Built-in Rules

Union Inputs: @oneOf

This directive provides validation for input types and implements the concept of union inputs. You can find the complete spec RFC here.

You can use union inputs either via a the SDL flow, by annotating types and fields with @oneOf or via the extensions field.

First, make sure to add that rule to your plugin usage:

import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
import { envelop, useEngine } from '@envelop/core'
import { OneOfInputObjectsRule, useExtendedValidation } from '@envelop/extended-validation'

const getEnveloped = envelop({
  plugins: [
    useEngine({ parse, validate, specifiedRules, execute, subscribe }),
    useExtendedValidation({
      rules: [OneOfInputObjectsRule]
    })
  ]
})

Schema Directive Flow

Make sure to include the following directive in your schema:

directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION

Then, apply it to field definitions, or to a complete input type:

## Apply to entire input type
input FindUserInput @oneOf {
  id: ID
  organizationAndRegistrationNumber: GraphQLInt
}

## Or, apply to a set of input arguments

type Query {
  foo(id: ID, str1: String, str2: String): String @oneOf
}

Programmatic extensions flow

const GraphQLFindUserInput = new GraphQLInputObjectType({
  name: 'FindUserInput',
  fields: {
    id: {
      type: GraphQLID
    },
    organizationAndRegistrationNumber: {
      type: GraphQLInt
    }
  },
  extensions: {
    oneOf: true
  }
})

const Query = new GraphQLObjectType({
  name: 'Query',
  fields: {
    foo: {
      type: GraphQLString,
      args: {
        id: {
          type: GraphQLID
        },
        str1: {
          type: GraphQLString
        },
        str2: {
          type: GraphQLString
        }
      },
      extensions: {
        oneOf: true
      }
    }
  }
})