graphql-validate-directive
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -76,5 +76,3 @@ import { isInputObjectType, isScalarType, GraphQLInt, GraphQLFloat, GraphQLString, isListType, isNonNullType, } from 'graphql'; | ||
const directive = getDirective(this.schema, field, this.directiveName); | ||
if (!directive) | ||
continue; | ||
properties[name] = this.getInputTypeSchema(field.type, directive[0]); | ||
properties[name] = this.getInputTypeSchema(field.type, directive?.[0]); | ||
} | ||
@@ -94,2 +92,3 @@ return jsonSchema; | ||
const jsonSchema = { | ||
$async: true, | ||
type: 'object', | ||
@@ -114,6 +113,4 @@ properties, | ||
const originalResolve = field.resolve; | ||
field.resolve = function resolverWithValidator(...args) { | ||
validate(args[1]); | ||
if (validate.errors?.[0]) | ||
throw validate.errors[0]; | ||
field.resolve = async function resolverWithValidator(...args) { | ||
await validate(args[1]); | ||
return originalResolve?.apply(this, args); | ||
@@ -120,0 +117,0 @@ }; |
@@ -15,4 +15,4 @@ export const GraphQLValidateDirectiveTypeDefs = `#graphql | ||
uniqueItems: Boolean | ||
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION | ||
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION | ||
`; | ||
//# sourceMappingURL=typeDefs.js.map |
{ | ||
"name": "graphql-validate-directive", | ||
"version": "0.1.4", | ||
"description": "A GraphQL directive for input validation", | ||
"version": "0.1.5", | ||
"main": "dist/cjs/index.js", | ||
@@ -14,4 +15,4 @@ "module": "dist/esm/index.js", | ||
"@types/lodash": "^4.14.180", | ||
"@types/mocha": "^8.2.1", | ||
"@types/node": "^14.14.31", | ||
"apollo-server": "^3.6.6", | ||
"codecov": "^3.8.3", | ||
@@ -21,3 +22,2 @@ "eslint": "^7.21.0", | ||
"jest": "^26.6.3", | ||
"mocha": "^8.3.1", | ||
"ts-jest": "^26.5.4", | ||
@@ -31,3 +31,3 @@ "typescript": "^4.6.3" | ||
"test": "jest", | ||
"prepublish": "yarn build" | ||
"prepublishOnly": "yarn build" | ||
}, | ||
@@ -38,3 +38,6 @@ "jest": { | ||
"^.+\\.ts$": "ts-jest" | ||
} | ||
}, | ||
"collectCoverageFrom": [ | ||
"src/**/*.ts" | ||
] | ||
}, | ||
@@ -46,3 +49,12 @@ "dependencies": { | ||
"lodash": "^4.17.21" | ||
} | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^16.3.0" | ||
}, | ||
"keywords": [ | ||
"graphql", | ||
"directive", | ||
"validate", | ||
"validation" | ||
] | ||
} |
@@ -28,2 +28,4 @@ # graphql-validate-directive | ||
Use in the project. | ||
```ts | ||
@@ -48,1 +50,28 @@ import { | ||
``` | ||
Use in schema. | ||
```graphql | ||
# used on input field | ||
input PostInput { | ||
title: String! @validate(minLength: 1, maxLength: 1000) | ||
content: String! @validate(minLength: 1, maxLength: 1000) | ||
# used on array | ||
tags: [String!] | ||
@validate(minItems: 1, maxItems: 10, minLength: 1, maxLength: 100) | ||
} | ||
type Mutation { | ||
createPost(input: PostInput!): Post | ||
# used on argument | ||
createMessage( | ||
content: String! @validate(minLength: 1, maxLength: 200) | ||
): Message | ||
} | ||
``` | ||
### License | ||
MIT Licensed. |
@@ -22,5 +22,5 @@ import { | ||
import pick from 'lodash/pick' | ||
import { Maybe } from 'graphql/jsutils/Maybe' | ||
import type { Maybe } from 'graphql/jsutils/Maybe' | ||
import { DefaultValidateDirectiveName } from './constants' | ||
import { ValidateOptions } from './type' | ||
import type { ValidateOptions } from './type' | ||
import { | ||
@@ -43,3 +43,2 @@ KeywordsForArray, | ||
): JSONSchemaType<any> { | ||
// not validate custom scaler | ||
if (type === GraphQLInt) { | ||
@@ -66,2 +65,5 @@ return { | ||
// not validate custom scaler | ||
// return empty schema to match anything | ||
/* istanbul ignore next */ | ||
return {} as any | ||
@@ -119,4 +121,3 @@ } | ||
const directive = getDirective(this.schema, field, this.directiveName) | ||
if (!directive) continue | ||
properties[name] = this.getInputTypeSchema(field.type, directive[0]) | ||
properties[name] = this.getInputTypeSchema(field.type, directive?.[0]) | ||
} | ||
@@ -139,2 +140,3 @@ return jsonSchema | ||
const jsonSchema: JSONSchemaType<Record<string, any>> = { | ||
$async: true, | ||
type: 'object', | ||
@@ -162,5 +164,4 @@ properties, | ||
const originalResolve = field.resolve | ||
field.resolve = function resolverWithValidator(...args) { | ||
validate(args[1]) | ||
if (validate.errors?.[0]) throw validate.errors[0] | ||
field.resolve = async function resolverWithValidator(...args) { | ||
await validate(args[1]) | ||
return originalResolve?.apply(this, args) | ||
@@ -171,2 +172,5 @@ } | ||
public composeResolver(type: Maybe<GraphQLObjectType>) { | ||
// it's useless to applied with null or undefined | ||
// add this check for safety and convenience to apply | ||
/* istanbul ignore next */ | ||
if (!type) return | ||
@@ -173,0 +177,0 @@ const fields = type.getFields() |
@@ -27,2 +27,4 @@ import { | ||
// following code is used to validate the type declarations | ||
/* istanbul ignore file */ | ||
const _assertType = <T extends true>(t?: T) => {} | ||
@@ -29,0 +31,0 @@ |
@@ -15,3 +15,3 @@ export const GraphQLValidateDirectiveTypeDefs = `#graphql | ||
uniqueItems: Boolean | ||
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION | ||
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION | ||
` |
import { GraphQLSchema, GraphQLNamedType, GraphQLInputType, GraphQLScalarType, GraphQLList, GraphQLObjectType, GraphQLField, GraphQLArgument } from 'graphql'; | ||
import Ajv from 'ajv'; | ||
import type { JSONSchemaType } from 'ajv'; | ||
import { Maybe } from 'graphql/jsutils/Maybe'; | ||
import { ValidateOptions } from './type'; | ||
import type { Maybe } from 'graphql/jsutils/Maybe'; | ||
import type { ValidateOptions } from './type'; | ||
export declare class GraphQLValidateDirective { | ||
@@ -7,0 +7,0 @@ schema: GraphQLSchema; |
@@ -1,1 +0,1 @@ | ||
export declare const GraphQLValidateDirectiveTypeDefs = "#graphql\ndirective @validate(\n maximum: Float\n minimum: Float\n exclusiveMaximum: Float\n exclusiveMinimum: Float\n multipleOf: Float\n maxLength: Int\n minLength: Int\n pattern: String\n format: String\n maxItems: Int\n minItems: Int\n uniqueItems: Boolean\n) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | ARGUMENT_DEFINITION\n"; | ||
export declare const GraphQLValidateDirectiveTypeDefs = "#graphql\ndirective @validate(\n maximum: Float\n minimum: Float\n exclusiveMaximum: Float\n exclusiveMinimum: Float\n multipleOf: Float\n maxLength: Int\n minLength: Int\n pattern: String\n format: String\n maxItems: Int\n minItems: Int\n uniqueItems: Boolean\n) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION\n"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
41665
11
730
76
5