0.2.0 • Published 3 years ago

nexus-validation-plugin v0.2.0

Weekly downloads
26
License
MIT
Repository
github
Last release
3 years ago

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()],
});
0.2.0

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.2-alpha2

3 years ago

0.1.2-alpha1

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago