New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I18n for validation error messages #2878
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7726af0
Add I18n and locale to validation
dondonz 6c20104
Add bundles
dondonz e74c883
Add bundle read
dondonz 201f3d9
Add i18n to validation rules, add test coverage, update tests
dondonz 80004db
Add i18n to argument validation
dondonz 3f9e9be
Fix coercing error
dondonz 89c5728
Remove unused message
dondonz d1713ec
Formally deprecate test only validation errors
dondonz 941bf38
Rename file
dondonz e5f5120
Add i18n bundle tests
dondonz a7493b6
Tidy javadoc
dondonz d204c2b
Merge branch 'master' into validation-error-refactor
dondonz 47e3e3a
Rename method
dondonz ce5908b
Add back non-breaking validate methods, which will use the default lo…
dondonz af13dd5
Add defaulted locale test
dondonz File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import graphql.validation.Validator; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
|
@@ -40,7 +41,7 @@ public class ParseAndValidate { | |
public static ParseAndValidateResult parseAndValidate(GraphQLSchema graphQLSchema, ExecutionInput executionInput) { | ||
ParseAndValidateResult result = parse(executionInput); | ||
if (!result.isFailure()) { | ||
List<ValidationError> errors = validate(graphQLSchema, result.getDocument()); | ||
List<ValidationError> errors = validate(graphQLSchema, result.getDocument(), executionInput.getLocale()); | ||
return result.transform(builder -> builder.validationErrors(errors)); | ||
} | ||
return result; | ||
|
@@ -71,11 +72,24 @@ public static ParseAndValidateResult parse(ExecutionInput executionInput) { | |
* | ||
* @param graphQLSchema the graphql schema to validate against | ||
* @param parsedDocument the previously parsed document | ||
* @param locale the current locale | ||
* | ||
* @return a result object that indicates how this operation went | ||
*/ | ||
public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument, Locale locale) { | ||
return validate(graphQLSchema, parsedDocument, ruleClass -> true, locale); | ||
} | ||
|
||
/** | ||
* This can be called to validate a parsed graphql query, with the JVM default locale. | ||
* | ||
* @param graphQLSchema the graphql schema to validate against | ||
* @param parsedDocument the previously parsed document | ||
* | ||
* @return a result object that indicates how this operation went | ||
*/ | ||
public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument) { | ||
return validate(graphQLSchema, parsedDocument, ruleClass -> true); | ||
return validate(graphQLSchema, parsedDocument, ruleClass -> true, Locale.getDefault()); | ||
} | ||
|
||
/** | ||
|
@@ -84,11 +98,26 @@ public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Docume | |
* @param graphQLSchema the graphql schema to validate against | ||
* @param parsedDocument the previously parsed document | ||
* @param rulePredicate this predicate is used to decide what validation rules will be applied | ||
* @param locale the current locale | ||
* | ||
* @return a result object that indicates how this operation went | ||
*/ | ||
public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument, Predicate<Class<?>> rulePredicate, Locale locale) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same breaking change- leave the old, use Locale.getDefault() and win |
||
Validator validator = new Validator(); | ||
return validator.validateDocument(graphQLSchema, parsedDocument, rulePredicate, locale); | ||
} | ||
|
||
/** | ||
* This can be called to validate a parsed graphql query, with the JVM default locale. | ||
* | ||
* @param graphQLSchema the graphql schema to validate against | ||
* @param parsedDocument the previously parsed document | ||
* @param rulePredicate this predicate is used to decide what validation rules will be applied | ||
* | ||
* @return a result object that indicates how this operation went | ||
*/ | ||
public static List<ValidationError> validate(GraphQLSchema graphQLSchema, Document parsedDocument, Predicate<Class<?>> rulePredicate) { | ||
Validator validator = new Validator(); | ||
return validator.validateDocument(graphQLSchema, parsedDocument, rulePredicate); | ||
return validator.validateDocument(graphQLSchema, parsedDocument, rulePredicate, Locale.getDefault()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package graphql.i18n; | ||
|
||
import graphql.Internal; | ||
import graphql.VisibleForTesting; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import java.util.ResourceBundle; | ||
|
||
import static graphql.Assert.assertNotNull; | ||
import static graphql.Assert.assertShouldNeverHappen; | ||
|
||
@Internal | ||
public class I18n { | ||
|
||
/** | ||
* This enum is a type safe way to control what resource bundle to load from | ||
*/ | ||
public enum BundleType { | ||
Validation, | ||
Execution, | ||
General; | ||
|
||
private final String baseName; | ||
|
||
BundleType() { | ||
this.baseName = "i18n." + this.name(); | ||
} | ||
} | ||
|
||
private final ResourceBundle resourceBundle; | ||
|
||
@VisibleForTesting | ||
protected I18n(BundleType bundleType, Locale locale) { | ||
assertNotNull(bundleType); | ||
assertNotNull(locale); | ||
this.resourceBundle = ResourceBundle.getBundle(bundleType.baseName, locale); | ||
} | ||
|
||
public ResourceBundle getResourceBundle() { | ||
return resourceBundle; | ||
} | ||
|
||
public static I18n i18n(BundleType bundleType, Locale locale) { | ||
return new I18n(bundleType, locale); | ||
} | ||
|
||
|
||
/** | ||
* Creates an I18N message using the key and arguments | ||
* | ||
* @param msgKey the key in the underlying message bundle | ||
* @param msgArgs the message arguments | ||
* | ||
* @return the formatted I18N message | ||
*/ | ||
@SuppressWarnings("UnnecessaryLocalVariable") | ||
public String msg(String msgKey, Object... msgArgs) { | ||
String msgPattern = null; | ||
try { | ||
msgPattern = resourceBundle.getString(msgKey); | ||
} catch (MissingResourceException e) { | ||
assertShouldNeverHappen("There must be a resource bundle key called %s", msgKey); | ||
} | ||
|
||
String formattedMsg = new MessageFormat(msgPattern).format(msgArgs); | ||
return formattedMsg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package graphql.i18n; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
/** | ||
* A class that represents the intention to create a I18n message | ||
*/ | ||
public class I18nMsg { | ||
private final String msgKey; | ||
private final List<Object> msgArguments; | ||
|
||
public I18nMsg(String msgKey, List<Object> msgArguments) { | ||
this.msgKey = msgKey; | ||
this.msgArguments = msgArguments; | ||
} | ||
|
||
public I18nMsg(String msgKey, Object... msgArguments) { | ||
this.msgKey = msgKey; | ||
this.msgArguments = asList(msgArguments); | ||
} | ||
|
||
public String getMsgKey() { | ||
return msgKey; | ||
} | ||
|
||
public Object[] getMsgArguments() { | ||
return msgArguments.toArray(); | ||
} | ||
|
||
public I18nMsg addArgumentAt(int index, Object argument) { | ||
List<Object> newArgs = new ArrayList<>(this.msgArguments); | ||
newArgs.add(index, argument); | ||
return new I18nMsg(this.msgKey, newArgs); | ||
} | ||
|
||
public String toI18n(I18n i18n) { | ||
return i18n.msg(msgKey, msgArguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change. Leave the old and use Locale.getDefault() in it and delegate to the old method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, I'll change this