graphql-validate-fixtures
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -85,3 +85,3 @@ "use strict"; | ||
function validateValueAgainstObjectFieldDescription(value, fieldDescription, keyPath, ast) { | ||
if (typeof value !== 'object' || Array.isArray(value)) { | ||
if (value == null || typeof value !== 'object' || Array.isArray(value)) { | ||
return []; | ||
@@ -145,6 +145,18 @@ } | ||
} | ||
if (value == null) { | ||
if (value === null) { | ||
return []; | ||
} | ||
const valueType = typeof value; | ||
if (type instanceof graphql_1.GraphQLList) { | ||
if (!Array.isArray(value)) { | ||
return [error(keyPath, `should be an array (or null), but was ${articleForType(valueType)} ${valueType}`)]; | ||
} | ||
return shallow | ||
? [] | ||
: value.reduce((allErrors, item, index) => (allErrors.concat(validateValueAgainstType(item, type.ofType, updateKeyPath(keyPath, index), options))), []); | ||
} | ||
if (value === undefined) { | ||
const typeName = nameForType(type); | ||
return [error(keyPath, `should be ${articleForType(typeName)} ${typeName} (or null), but was undefined`)]; | ||
} | ||
if (type instanceof graphql_1.GraphQLObjectType) { | ||
@@ -172,10 +184,2 @@ if (valueType === 'object') { | ||
} | ||
if (type instanceof graphql_1.GraphQLList) { | ||
if (!Array.isArray(value)) { | ||
return [error(keyPath, `should be an array, but was a ${valueType}`)]; | ||
} | ||
return shallow | ||
? [] | ||
: value.reduce((allErrors, item, index) => (allErrors.concat(validateValueAgainstType(item, type.ofType, updateKeyPath(keyPath, index), options))), []); | ||
} | ||
if (type === graphql_1.GraphQLString) { | ||
@@ -195,7 +199,2 @@ return valueType === 'string' | ||
else if (type === graphql_1.GraphQLFloat) { | ||
if (typeof value === 'number') { | ||
return Number.isInteger(value) | ||
? [error(keyPath, 'should be a float but was an integer')] | ||
: []; | ||
} | ||
return [error(keyPath, `should be a float but was a ${valueType}`)]; | ||
@@ -211,3 +210,3 @@ } | ||
? [] | ||
: [error(keyPath, `value does not match scalar ${type.name}`)]; | ||
: [error(keyPath, `value does not match scalar ${nameForType(type)}`)]; | ||
} | ||
@@ -217,6 +216,25 @@ if (type instanceof graphql_1.GraphQLEnumType) { | ||
? [] | ||
: [error(keyPath, `value does not match enum ${type.name} (available values: ${type.getValues().map((enumValue) => enumValue.value).join(', ')})`)]; | ||
: [error(keyPath, `value does not match enum ${nameForType(type)} (available values: ${type.getValues().map((enumValue) => enumValue.value).join(', ')})`)]; | ||
} | ||
return []; | ||
} | ||
const CUSTOM_NAMES = { | ||
[graphql_1.GraphQLBoolean.name]: 'boolean', | ||
[graphql_1.GraphQLFloat.name]: 'float', | ||
[graphql_1.GraphQLInt.name]: 'integer', | ||
[graphql_1.GraphQLString.name]: 'string', | ||
}; | ||
function nameForType(type) { | ||
return CUSTOM_NAMES.hasOwnProperty(type.name) ? CUSTOM_NAMES[type.name] : type.name; | ||
} | ||
const TYPES_WITH_ARTICLE_AN = [ | ||
'object', | ||
'integer', | ||
'array', | ||
]; | ||
function articleForType(type) { | ||
return TYPES_WITH_ARTICLE_AN.includes(type) | ||
? 'an' | ||
: 'a'; | ||
} | ||
function updateKeyPath(keyPath, newKey) { | ||
@@ -223,0 +241,0 @@ if (typeof newKey === 'number') { |
{ | ||
"name": "graphql-validate-fixtures", | ||
"description": "Validates JSON fixtures for GraphQL responses against the associated operations and schema", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46322
424