nexus-validation-plugin
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Nexus Validation Plugin

nexus-validation-plugin is a Nexus plugin for argument validation.

Before executing a field resolver, nexus-validation-plugin will look for the validation config object. If validation object exists, the plugin will run each validation function and the resolve function only gets called when all validation passes. If any one of the validation function fails, field resolution throws an error stating which argument is invalid.

Installation

npm install nexus-validation-plugin

Example

import { mutationField, nonNull, stringArg } from 'nexus';
import { validationPlugin } from 'nexus-validation-plugin';

const signInEmail = mutationField('signInEmail', {
  type: nonNull('User'),

  args: {
    email: nonNull(stringArg()),
    password: nonNull(stringArg()),
  },

  validation: {
    email: async (value) => /.*@.*/.test(value), // Should contain @.
    password: async (value) => value !== '', // Should not be empty.
  },

  resolve: async (parent, args, ctx) => {
    // Resolver only runs after successful validation.
    // ...
  }
});

const schema = makeSchema({
  [signInEmail],
  plugins: [validationPlugin()],
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.0
    163
    • latest

Version History

Package Sidebar

Install

npm i nexus-validation-plugin

Weekly Downloads

165

Version

0.2.0

License

MIT

Unpacked Size

11.4 kB

Total Files

12

Last publish

Collaborators

  • dooboolab