graphql-constraint-directive
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "graphql-constraint-directive", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Validate GraphQL fields", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -80,2 +80,3 @@ # graphql-constraint-directive | ||
- date-time: RFC 3339 | ||
- date: ISO 8601 | ||
@@ -82,0 +83,0 @@ - ipv4 |
@@ -7,3 +7,3 @@ const { GraphQLError } = require('graphql/error') | ||
throw new GraphQLError('Must be a date in RFC 3339 format') | ||
throw new GraphQLError('Must be a date-time in RFC 3339 format') | ||
} |
const formats = [ | ||
'byte', | ||
'date-time', | ||
'date', | ||
'email', | ||
@@ -5,0 +6,0 @@ 'ipv4', |
@@ -531,3 +531,3 @@ const { deepStrictEqual, strictEqual } = require('assert') | ||
strictEqual(body.errors[0].message, | ||
'Variable "$input" got invalid value {"title":"a"}; Expected type ConstraintString at value.title; Must be a date in RFC 3339 format') | ||
'Variable "$input" got invalid value {"title":"a"}; Expected type ConstraintString at value.title; Must be a date-time in RFC 3339 format') | ||
}) | ||
@@ -544,3 +544,3 @@ | ||
deepStrictEqual(body.errors[0], { | ||
message: 'Must be a date in RFC 3339 format', | ||
message: 'Must be a date-time in RFC 3339 format', | ||
code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION', | ||
@@ -553,2 +553,63 @@ fieldName: 'title', | ||
describe('#date', function () { | ||
before(function () { | ||
this.typeDefs = ` | ||
type Query { | ||
books: [Book] | ||
} | ||
type Book { | ||
title: String | ||
} | ||
type Mutation { | ||
createBook(input: BookInput): Book | ||
} | ||
input BookInput { | ||
title: String! @constraint(format: "date") | ||
}` | ||
this.request = setup(this.typeDefs) | ||
}) | ||
it('should pass', async function () { | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ | ||
query, variables: { input: { title: '2018-05-16' } } | ||
}) | ||
strictEqual(statusCode, 200) | ||
deepStrictEqual(body, { data: { createBook: null } }) | ||
}) | ||
it('should fail', async function () { | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ | ||
query, variables: { input: { title: 'a' } } | ||
}) | ||
strictEqual(statusCode, 400) | ||
strictEqual(body.errors[0].message, | ||
'Variable "$input" got invalid value {"title":"a"}; Expected type ConstraintString at value.title; Must be a date in ISO 8601 format') | ||
}) | ||
it('should throw custom error', async function () { | ||
const request = setup(this.typeDefs, formatError) | ||
const { body, statusCode } = await request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query, variables: { input: { title: 'a' } } }) | ||
strictEqual(statusCode, 400) | ||
deepStrictEqual(body.errors[0], { | ||
message: 'Must be a date in ISO 8601 format', | ||
code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION', | ||
fieldName: 'title', | ||
context: [{ arg: 'format', value: 'date' }] | ||
}) | ||
}) | ||
}) | ||
describe('#email', function () { | ||
@@ -555,0 +616,0 @@ before(function () { |
55730
23
1443
131