All Projects → confuser → Graphql Constraint Directive

confuser / Graphql Constraint Directive

Licence: isc
Validate GraphQL fields

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Graphql Constraint Directive

python-valid8
Yet another validation lib ;). Provides tools for general-purpose variable validation, function inputs/outputs validation as well as class fields validation. All entry points raise consistent ValidationError including all contextual details, with dynamic inheritance of ValueError/TypeError as appropriate.
Stars: ✭ 24 (-94.01%)
Mutual labels:  validation, validator
validation
Validation on Laravel 5.X|6.X|7.X|8.X
Stars: ✭ 26 (-93.52%)
Mutual labels:  validation, validator
NZ-Bank-Account-Validator
A small, zero dependency NZ bank account validation library that runs everywhere.
Stars: ✭ 15 (-96.26%)
Mutual labels:  validation, validator
openapi-lint-vscode
OpenAPI 2.0/3.0.x intellisense, validator, linter, converter and resolver extension for Visual Studio Code
Stars: ✭ 47 (-88.28%)
Mutual labels:  validation, validator
Validate
A simple jQuery plugin to validate forms.
Stars: ✭ 298 (-25.69%)
Mutual labels:  validation, validator
dockerfile-utils
A library and command line interface for formatting and linting Dockerfiles.
Stars: ✭ 17 (-95.76%)
Mutual labels:  validation, validator
thai-citizen-id-validator
🦉 Validate Thai Citizen ID with 0 dependencies 🇹🇭
Stars: ✭ 35 (-91.27%)
Mutual labels:  validation, validator
Maat
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.
Stars: ✭ 27 (-93.27%)
Mutual labels:  validation, validator
Validate
⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。
Stars: ✭ 378 (-5.74%)
Mutual labels:  validation, validator
js-form-validator
Javascript form validation. Pure JS. No jQuery
Stars: ✭ 38 (-90.52%)
Mutual labels:  validation, validator
datalize
Parameter, query, form data validation and filtering for NodeJS.
Stars: ✭ 55 (-86.28%)
Mutual labels:  validation, validator
Validator.js
String validation
Stars: ✭ 18,842 (+4598.75%)
Mutual labels:  validation, validator
filter
Go语言的数据过滤包,由 数据输入、格式化、校验、输出 几个部份组成。
Stars: ✭ 22 (-94.51%)
Mutual labels:  validation, validator
verum-php
Server-Side Validation Library for PHP
Stars: ✭ 17 (-95.76%)
Mutual labels:  validation, validator
pyvaru
Rule based data validation library for python 3.
Stars: ✭ 17 (-95.76%)
Mutual labels:  validation, validator
checker
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Stars: ✭ 62 (-84.54%)
Mutual labels:  validation, validator
node-input-validator
Validation library for node.js
Stars: ✭ 74 (-81.55%)
Mutual labels:  validation, validator
openui5-validator
A library to validate OpenUI5 fields
Stars: ✭ 17 (-95.76%)
Mutual labels:  validation, validator
excel validator
Python script to validate data in Excel files
Stars: ✭ 14 (-96.51%)
Mutual labels:  validation, validator
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: ✭ 321 (-19.95%)
Mutual labels:  validation, validator

graphql-constraint-directive

Build Status Coverage Status Known Vulnerabilities

Allows using @constraint as a directive to validate input data. Inspired by Constraints Directives RFC and OpenAPI

Install

npm install graphql-constraint-directive

Usage

const { constraintDirective, constraintDirectiveTypeDefs } = require('graphql-constraint-directive')
const express = require('express')
const { ApolloServer } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphql-tools')
const typeDefs = `
  type Query {
    books: [Book]
  }
  type Book {
    title: String
  }
  type Mutation {
    createBook(input: BookInput): Book
  }
  input BookInput {
    title: String! @constraint(minLength: 5, format: "email")
  }`
const schema = makeExecutableSchema({
  typeDefs: [constraintDirectiveTypeDefs, typeDefs],
  schemaTransforms: [constraintDirective()]
})
const app = express()
const server = new ApolloServer({ schema })

server.applyMiddleware({ app })

API

String

minLength

@constraint(minLength: 5) Restrict to a minimum length

maxLength

@constraint(maxLength: 5) Restrict to a maximum length

startsWith

@constraint(startsWith: "foo") Ensure value starts with foo

endsWith

@constraint(endsWith: "foo") Ensure value ends with foo

contains

@constraint(contains: "foo") Ensure value contains foo

notContains

@constraint(notContains: "foo") Ensure value does not contain foo

pattern

@constraint(pattern: "^[0-9a-zA-Z]*$") Ensure value matches regex, e.g. alphanumeric

format

@constraint(format: "email") Ensure value is in a particular format

Supported formats:

  • byte: Base64
  • date-time: RFC 3339
  • date: ISO 8601
  • email
  • ipv4
  • ipv6
  • uri
  • uuid

Int/Float

min

@constraint(min: 3) Ensure value is greater than or equal to

max

@constraint(max: 3) Ensure value is less than or equal to

exclusiveMin

@constraint(exclusiveMin: 3) Ensure value is greater than

exclusiveMax

@constraint(exclusiveMax: 3) Ensure value is less than

multipleOf

@constraint(multipleOf: 10) Ensure value is a multiple

ConstraintDirectiveError

Each validation error throws a ConstraintDirectiveError. Combined with a formatError function, this can be used to customise error messages.

{
  code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION',
  fieldName: 'theFieldName',
  context: [ { arg: 'argument name which failed', value: 'value of argument' } ]
}
const formatError = function (error) {
  if (error.originalError && error.originalError.code === 'ERR_GRAPHQL_CONSTRAINT_VALIDATION') {
    // return a custom object
  }

  return error
}

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema, formatError }))

uniqueTypeName

@constraint(uniqueTypeName: "Unique_Type_Name") Override the unique type name generate by the library to the one passed as an argument

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].