0.1.3 • Published 7 months ago

postgraphile-plugin-js-validation v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

postgraphile-plugin-js-validation

Postgraphile Plugin that allows mutation validation before hitting the database. ✨

⚠️ Don't see this Plugin as an excuse to skip defining proper constraints in your database.

Table of Contents

Introduction

TODO

Install

npm install postgraphile-plugin-js-validation

or 

yarn add postgraphile-plugin-js-validation

Examples

In our example we already have a mutation called authUser.

TODO

Basic Usage

// import
import { ValidationPlugin, handleErrors } from "postgraphile-plugin-js-validation";

// start postgraphile
postgraphile(process.env.DATABASE_URL, schemaName, {
  // ... options here
  handleErrors: handleErrors(),
  appendPlugins: [
    ValidationPlugin({
      authUser: async (input) => {
        // your validation logic
	// return an error object or {} / undefined
        return {
          "email": "Email is required",
	  "password": "Password is required"
        };
      },
    }),
  ],
})

Usage With Yup

import { ValidationPlugin, handleErrors } from "postgraphile-plugin-js-validation";
import { YupValidator } from "postgraphile-plugin-js-validation/dist/YupValidator";

postgraphile(process.env.DATABASE_URL, schemaName, {
  // ... options here
  handleErrors: handleErrors("shape"),
  appendPlugins: [
    ValidationPlugin({
      authUser: YupValidator(
        async (info) =>
          yup.object().shape({
            email: yup.string().email().required("Email is required"),
            password: yup.string().required("Password is required")
          }),
        { abortEarly: false }
      ),
    }),
  ],
})
);

And here is how the result looks like.

{
  "errors": [
    {
      "validation": {
        "email": "Email is required",
	"password": "Password is required",
      }
    }
  ],
  "data": {
    "authUser": null
  }
}
0.1.3

7 months ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago