1- import { defaultFieldResolver , GraphQLSchema } from " graphql" ;
2- import { mapSchema , MapperKind , getDirective , getDirectives } from '@graphql-tools/utils'
3- import { UserInputError } from " apollo-server-express" ;
4- import { BooleanValueNode } from " graphql/language/ast" ;
1+ import { defaultFieldResolver , GraphQLSchema } from ' graphql' ;
2+ import { mapSchema , MapperKind , getDirective , getDirectives } from '@graphql-tools/utils' ;
3+ import { UserInputError } from ' apollo-server-express' ;
4+ import { BooleanValueNode } from ' graphql/language/ast' ;
55
66/**
77 * Validates string using regex
@@ -12,8 +12,8 @@ function checkEmail(email: string): void {
1212 const emailRegex = / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " .+ " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } ] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / g;
1313
1414 if ( email . match ( emailRegex ) === null ) {
15- throw new UserInputError ( 'Wrong email format' ) ;
16- }
15+ throw new UserInputError ( 'Wrong email format' ) ;
16+ }
1717}
1818
1919/**
@@ -22,13 +22,21 @@ function checkEmail(email: string): void {
2222 */
2323function checkNotEmpty ( str : string ) : void {
2424 if ( str . replace ( / \s / g, '' ) . length == 0 ) {
25- throw new UserInputError ( 'The value must not be empty' ) ;
26- }
25+ throw new UserInputError ( 'The value must not be empty' ) ;
26+ }
2727}
2828
29- export default function validateDirective ( directiveName = 'validate' ) {
29+ /**
30+ *
31+ * @param directiveName
32+ * @returns
33+ */
34+ export default function validateDirective ( directiveName = 'validate' ) : {
35+ validateDirectiveTypeDefs : string ;
36+ validateDirectiveTransformer : ( schema : GraphQLSchema ) => GraphQLSchema ;
37+ } {
3038 return {
31- validateDirectiveTypeDefs :`
39+ validateDirectiveTypeDefs : `
3240 """
3341 Directive for checking a field for empty space
3442 """
@@ -38,32 +46,38 @@ export default function validateDirective(directiveName = 'validate') {
3846 mapSchema ( schema , {
3947 [ MapperKind . MUTATION_ROOT_FIELD ] : ( fieldConfig , fieldName ) => {
4048 const args = fieldConfig . astNode ?. arguments ;
49+
4150 if ( args ) {
4251 args . forEach ( arg => {
4352 const directives = arg . directives ;
53+
4454 directives ?. forEach ( directive => {
4555 if ( directive . name . value === directiveName ) {
4656 const directiveArguments = directive . arguments ;
4757 const isEmail = ( directiveArguments ?. find ( arg => arg . name . value === 'isEmail' ) ?. value as BooleanValueNode ) ?. value ;
4858 const notEmpty = ( directiveArguments ?. find ( arg => arg . name . value === 'notEmpty' ) ?. value as BooleanValueNode ) ?. value ;
59+
4960 if ( isEmail || notEmpty ) {
5061 const { resolve = defaultFieldResolver } = fieldConfig ;
62+
5163 fieldConfig . resolve = async ( object , args , context , info ) => {
5264 if ( isEmail ) {
5365 checkEmail ( args [ arg . name . value ] || '' ) ;
5466 }
5567 if ( notEmpty ) {
5668 checkNotEmpty ( args [ arg . name . value ] || '' ) ;
5769 }
70+
5871 return resolve ( object , args , context , info ) ;
5972 } ;
6073 }
6174 }
62- } )
63- } )
75+ } ) ;
76+ } ) ;
6477 }
65- return fieldConfig
66- }
67- } )
68- }
78+
79+ return fieldConfig ;
80+ } ,
81+ } ) ,
82+ } ;
6983}
0 commit comments