@@ -17,6 +17,7 @@ It has NOT yet been consumed by a production like project and hence its API usef
1717
1818TODO
1919
20+
2021# SDL @Directive constraints
2122
2223This library a series of directives that can be applied to field arguments and input type fields which will constrain their allowable values.
@@ -49,8 +50,81 @@ For example
4950In the example above , we have a `applications ` argument that takes at most 10 applications and within each `Application ` input object ,
5051the `name ` field must be at least 3 characters long and no more than 100 characters long to be considered valid .
5152
53+ # Java Expression Language (Java EL)
54+
55+ The `@Expression ` validation directive allows Java EL to be used to help build validation rules .
56+
57+ Java EL is a powerful expression syntax for expressing validation conditions .
58+
59+ Some simple sample Java EL expressions might be :
60+
61+ | EL Expression | Result |
62+ | ------------- | -------- |
63+ | `${1> (4/2)} ` | `false ` |
64+ | `${4.0>= 3} ` | `true ` |
65+ | `${100.0 == 100}` | `true ` |
66+ | `${(10*10) ne 100} ` | `false ` |
67+ | `${'a ' > 'b '}` | `false ` |
68+ | `${'hip ' lt 'hit '}` | `true ` |
69+ | `${4> 3} ` | `true ` |
70+ | `${1.2E4 + 1.4} ` | `12001.4` |
71+ | `${3 div 4}` | `0.75` |
72+ | `${10 mod 4}` | `2` |
73+ | `${((x, y) → x + y )(3, 5.5)}` | `8.5` |
74+ | `[1,2,3,4].stream ().sum ()` | `10` |
75+ | `[1,3,5,2].stream ().sorted ().toList ()` | `[1, 2, 3, 5]` |
76+
77+ The following validation variables are made available to you :
78+
79+ | Name | Value |
80+ | ------------- | ----- |
81+ | `validatedValue ` | The value being validated |
82+ | `gqlField ` | The `GraphQLFieldDefinition ` being validated |
83+ | `gqlFieldContainer ` | The `GraphQLFieldsContainer ` parent type containing that field |
84+ | `gqlArgument ` | The `GraphQLArgument ` being validated . This can be null for field level validation |
85+ | `arguments ` | The map of all argument values for the current field |
86+ | `args ` | A short hand name for the map of all argument values for the current field |
87+
88+ The Java EL expression MUST evaluate to a boolean value to be useful in the `@Expresion ` directive .
89+
90+ See here for [a more complete overview of Java EL ](https ://javaee .github .io /tutorial /jsf -el001 .html )
91+
92+
93+ # Message Interpolation
94+
95+ The validation code uses a `graphql .validation .interpolation .MessageInterpolator ` interface to build out error messages . A default
96+ `ResourceBundleMessageInterpolator ` class is provided to load error messages from Java resource bundles to allow internationalised messages (I18N)
97+
98+ You can use Java EL syntax in the message templates to format even more powerful error messages .
99+
100+ ```
101+ The field ${gqlField.name} has the following invalid value : ${formatter.format('%1$.2f', validatedValue)}
102+ ```
103+
104+ If you use directive arguments like `message : String = "graphql.validation.Size.message"` then the `ResourceBundleMessageInterpolator` class
105+ will use that as a resource bundle lookup key. This too is inspired by the javax.validation annotations and how they work.
106+
107+ Like javax.validation, this library ships with some default error message templates but you can override them.
108+
109+
110+ # Complex input types
111+
112+ You can put @Directive constraints on complex input types as well as simple field arguments
113+
114+ ```graphql
115+ input ProductItem {
116+ code : String @Size(max : 5)
117+ price : String @Size(max : 3)
118+ }
119+
120+ type Mutation {
121+ updateProduct( product : ID, items : [ProductItem]) : Product
122+ }
123+ ```
124+
125+ In the example above each ` ProductItem ` in the list of items is examined for valid values
52126
53- ## The supplied constraints
127+ ## The supplied @ Directive constraints
54128
55129<!-- generated by DocHelper on 2019-08-17T11:55:22.933Z -->
56130
0 commit comments