Skip to content

Commit 3629c71

Browse files
avalyconfuser
andauthored
feat: send args to custom format function (#267)
fixes #266 Co-authored-by: James Mortemore <[email protected]>
1 parent 7591f1d commit 3629c71

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

index.d.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,24 @@ interface DocumentationOptions {
3939
};
4040
}
4141

42+
interface StringArguments {
43+
maxLength?: number;
44+
minLength?: number;
45+
startsWith?: string;
46+
endWith?: string;
47+
contains?: string;
48+
notContains?: string;
49+
pattern?: string;
50+
}
51+
4252
interface PluginOptions {
43-
formats: Record<string, (value: unknown) => boolean>;
53+
formats: Record<string, (value: string, args?: StringArguments) => boolean>;
4454
}
4555

4656
/**
4757
* Schema transformer which adds @constraint directives documentation to the fields and arguments descriptions.
4858
* Documentation not added if it already exists (`header` is present in the field or argument description)
49-
*
59+
*
5060
* @param options options to customize the documentation process
5161
*/
5262
export function constraintDirectiveDocumentation (options: DocumentationOptions) : (schema: GraphQLSchema) => GraphQLSchema;
@@ -58,7 +68,7 @@ export const constraintDirectiveTypeDefs: string
5868

5969
/**
6070
* Method for query validation based on the @constraint directives defined in the schema.
61-
*
71+
*
6272
* @param schema GraphQL schema to look for directives
6373
* @param query GraphQL query to validate
6474
* @param variables used in the query to validate

scalars/string.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function validate (fieldName, args, value, options = {}) {
8686
}
8787

8888
try {
89-
formatter(value) // Will throw if invalid
89+
formatter(value, args) // Will throw if invalid
9090
} catch (e) {
9191
throw new ValidationError(fieldName,
9292
e.message,

test/string.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -923,13 +923,13 @@ module.exports.test = function (setup, implType) {
923923
createBook(input: BookInput): Book
924924
}
925925
input BookInput {
926-
title: String! @constraint(format: "test-format")
926+
title: String! @constraint(minLength: 1, format: "test-format")
927927
}`
928928

929-
const testFormat = (value) => {
929+
const testFormat = (value, args) => {
930930
if (value === 'this is a test value') return true
931931

932-
throw new GraphQLError('Must be in test format format')
932+
throw new GraphQLError(`Must be in test format format and have a minimum length of ${args.minLength}`)
933933
}
934934

935935
const pluginOptions = { formats: { 'test-format': testFormat } }
@@ -959,7 +959,7 @@ module.exports.test = function (setup, implType) {
959959

960960
isStatusCodeError(statusCode, implType)
961961
strictEqual(body.errors[0].message,
962-
'Variable "$input" got invalid value "a" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_format_test-format"') + '. Must be in test format format')
962+
'Variable "$input" got invalid value "a" at "input.title"' + valueByImplType(implType, '; Expected type "title_String_NotNull_format_test-format"') + '. Must be in test format format and have a minimum length of 1')
963963
})
964964
})
965965
}

0 commit comments

Comments
 (0)