graphql-constraint-directive
Advanced tools
Comparing version 4.1.0 to 4.1.1
@@ -189,2 +189,4 @@ const { getVariableValues } = require('graphql/execution/values.js') | ||
let hasNonListValidation = false | ||
if (directiveArgumentMap) { | ||
@@ -209,2 +211,9 @@ let errMessageBase | ||
} | ||
for (const key in directiveArgumentMap) { | ||
if (key !== 'maxItems' && key !== 'minItems') { | ||
hasNonListValidation = true | ||
break | ||
} | ||
} | ||
} | ||
@@ -219,3 +228,3 @@ | ||
validateInputTypeValue(context, valueTypeDefArray, argName, variableName, element, currentField, iFieldNameFullIndexed) | ||
} else { | ||
} else if (hasNonListValidation) { | ||
const atMessage = ` at "${iFieldNameFullIndexed}"` | ||
@@ -222,0 +231,0 @@ |
{ | ||
"name": "graphql-constraint-directive", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"description": "Validate GraphQL fields", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,3 @@ const { deepStrictEqual, strictEqual } = require('assert') | ||
describe('Array size', function () { | ||
describe('Input object', function () { | ||
describe('Inside Input Object', function () { | ||
const query = `mutation createBook($input: BookInput) { | ||
@@ -130,3 +130,3 @@ createBook(input: $input) { | ||
describe('Input argument', function () { | ||
describe('Scalar argument', function () { | ||
describe('#minItems', function () { | ||
@@ -214,3 +214,3 @@ before(async function () { | ||
type Mutation { | ||
createBook(input: [Int] @constraint(maxItems: 2)): Book | ||
createBook(input: [Int] @constraint(maxItems: 2, max: 100)): Book | ||
}` | ||
@@ -266,2 +266,3 @@ | ||
isStatusCodeError(statusCode, implType) | ||
strictEqual(body.errors.length, 1) | ||
strictEqual( | ||
@@ -274,5 +275,161 @@ body.errors[0].message, | ||
}) | ||
it('should fail - another scalar validation', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: [2, 101]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
isStatusCodeError(statusCode, implType) | ||
strictEqual(body.errors.length, 1) | ||
strictEqual( | ||
body.errors[0].message, | ||
'Argument "input" of "createBook" got invalid value 101 at "[1]"' + | ||
valueByImplType(implType, '; Expected type "title_List_Int_NotNull_max_3"') + | ||
'. Must be no greater than 100' | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('Scalar argument of ID type', function () { | ||
describe('#minItems', function () { | ||
before(async function () { | ||
this.typeDefs = ` | ||
type Query { | ||
books: [Book] | ||
} | ||
type Book { | ||
title: String | ||
} | ||
type Mutation { | ||
createBook(input: [ID]! @constraint(minItems: 3)): Book | ||
}` | ||
this.request = await setup(this.typeDefs) | ||
}) | ||
it('should pass', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: ["2","3","4"]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
strictEqual(statusCode, 200) | ||
deepStrictEqual(body, { data: { createBook: null } }) | ||
}) | ||
it('should fail', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: ["2","3"]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
isStatusCodeError(statusCode, implType) | ||
strictEqual(body.errors.length, 1) | ||
strictEqual( | ||
body.errors[0].message, | ||
'Argument "input" of "createBook"' + | ||
valueByImplType(implType, '; Expected type "title_List_ListNotNull_Int_NotNull_min_3"') + | ||
' must be at least 3 in length' | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('InputObject argument', function () { | ||
describe('#minItems', function () { | ||
before(async function () { | ||
this.typeDefs = ` | ||
type Query { | ||
books: [Book] | ||
} | ||
type Book { | ||
title: String | ||
} | ||
type Mutation { | ||
createBook(input: [BookInput]! @constraint(minItems: 3)): Book | ||
} | ||
input BookInput { | ||
title: String @constraint(maxLength: 2) | ||
} | ||
` | ||
this.request = await setup(this.typeDefs) | ||
}) | ||
it('should pass', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: [{title:"aa"},{title:"ab"},{title:"ba"}]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
strictEqual(statusCode, 200) | ||
deepStrictEqual(body, { data: { createBook: null } }) | ||
}) | ||
it('should fail', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: [{title:"aa"},{title:"ab"}]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
isStatusCodeError(statusCode, implType) | ||
strictEqual(body.errors.length, 1) | ||
strictEqual( | ||
body.errors[0].message, | ||
'Argument "input" of "createBook"' + | ||
valueByImplType(implType, '; Expected type "title_List_ListNotNull_Int_NotNull_min_3"') + | ||
' must be at least 3 in length' | ||
) | ||
}) | ||
it('should fail - another validation in input object', async function () { | ||
const query = `mutation createBook { | ||
createBook(input: [{title:"aa"},{title:"abc"},{title:"as"}]) { | ||
title | ||
} | ||
}` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
isStatusCodeError(statusCode, implType) | ||
strictEqual(body.errors.length, 1) | ||
strictEqual( | ||
body.errors[0].message, | ||
'Argument "input" of "createBook" got invalid value "abc" at "[1].title"' + | ||
valueByImplType(implType, '; Expected type "title_List_ListNotNull_Int_NotNull_min_3"') + | ||
'. Must be no more than 2 characters in length' | ||
) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
251727
6229