-
Notifications
You must be signed in to change notification settings - Fork 115
feat: validation error messages should use the framework translation … #1436
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
Open
h2zi
wants to merge
3
commits into
master
Choose a base branch
from
haozi/validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| package validation | ||
|
|
||
| import ( | ||
| "embed" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/fs" | ||
| ) | ||
|
|
||
| //go:embed lang | ||
| var LangFS embed.FS | ||
|
|
||
| func init() { | ||
| data, err := fs.ReadFile(LangFS, "lang/en/validation.json") | ||
| if err != nil { | ||
| panic(fmt.Sprintf("validation: failed to read embedded lang/en/validation.json: %v", err)) | ||
| } | ||
|
|
||
| var raw map[string]any | ||
| if err := json.Unmarshal(data, &raw); err != nil { | ||
| panic(fmt.Sprintf("validation: failed to parse embedded lang/en/validation.json: %v", err)) | ||
| } | ||
|
|
||
| defaultMessages = flattenMessages("", raw) | ||
| } | ||
|
|
||
| // flattenMessages flattens a nested JSON map into dot-separated keys. | ||
| // e.g. {"min": {"string": "...", "numeric": "..."}} → {"min.string": "...", "min.numeric": "..."} | ||
| func flattenMessages(prefix string, m map[string]any) map[string]string { | ||
| result := make(map[string]string) | ||
| for k, v := range m { | ||
| key := k | ||
| if prefix != "" { | ||
| key = prefix + "." + k | ||
| } | ||
| switch val := v.(type) { | ||
| case string: | ||
| result[key] = val | ||
| case map[string]any: | ||
| for fk, fv := range flattenMessages(key, val) { | ||
| result[fk] = fv | ||
| } | ||
| } | ||
| } | ||
| return result | ||
| } | ||
This file contains hidden or 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,176 @@ | ||
| { | ||
| "accepted": "The :attribute field must be accepted.", | ||
| "accepted_if": "The :attribute field must be accepted when :other is :value.", | ||
| "active_url": "The :attribute field must be a valid URL.", | ||
| "after": "The :attribute field must be a date after :date.", | ||
| "after_or_equal": "The :attribute field must be a date after or equal to :date.", | ||
| "alpha": "The :attribute field must only contain letters.", | ||
| "alpha_dash": "The :attribute field must only contain letters, numbers, dashes, and underscores.", | ||
| "alpha_num": "The :attribute field must only contain letters and numbers.", | ||
| "array": "The :attribute field must be an array.", | ||
| "ascii": "The :attribute field must only contain single-byte alphanumeric characters and symbols.", | ||
| "before": "The :attribute field must be a date before :date.", | ||
| "before_or_equal": "The :attribute field must be a date before or equal to :date.", | ||
| "between": { | ||
| "array": "The :attribute field must have between :min and :max items.", | ||
| "file": "The :attribute field must be between :min and :max kilobytes.", | ||
| "numeric": "The :attribute field must be between :min and :max.", | ||
| "string": "The :attribute field must be between :min and :max characters." | ||
| }, | ||
| "bool": "The :attribute field must be true or false.", | ||
| "boolean": "The :attribute field must be true or false.", | ||
| "confirmed": "The :attribute field confirmation does not match.", | ||
| "contains": "The :attribute field is missing a required value.", | ||
| "date": "The :attribute field must be a valid date.", | ||
| "date_equals": "The :attribute field must be a date equal to :date.", | ||
| "date_format": "The :attribute field must match the format :format.", | ||
| "decimal": "The :attribute field must have :decimal decimal places.", | ||
| "declined": "The :attribute field must be declined.", | ||
| "declined_if": "The :attribute field must be declined when :other is :value.", | ||
| "different": "The :attribute field and :other must be different.", | ||
| "digits": "The :attribute field must be :digits digits.", | ||
| "digits_between": "The :attribute field must be between :min and :max digits.", | ||
| "dimensions": "The :attribute field has invalid image dimensions.", | ||
| "distinct": "The :attribute field has a duplicate value.", | ||
| "doesnt_contain": "The :attribute field must not contain any of the following: :values.", | ||
| "doesnt_end_with": "The :attribute field must not end with one of the following: :values.", | ||
| "doesnt_start_with": "The :attribute field must not start with one of the following: :values.", | ||
| "email": "The :attribute field must be a valid email address.", | ||
| "encoding": "The :attribute field must be encoded as :values.", | ||
| "ends_with": "The :attribute field must end with one of the following: :values.", | ||
| "eq": "The :attribute field must be equal to :value.", | ||
| "exclude": "The :attribute field is excluded.", | ||
| "exclude_if": "The :attribute field is excluded when :other is :value.", | ||
| "exclude_unless": "The :attribute field is excluded unless :other is :value.", | ||
| "exclude_with": "The :attribute field is excluded when :values is present.", | ||
| "exclude_without": "The :attribute field is excluded when :values is not present.", | ||
| "exists": "The :attribute does not exist.", | ||
| "extensions": "The :attribute field must have one of the following extensions: :values.", | ||
| "file": "The :attribute field must be a file.", | ||
| "filled": "The :attribute field must have a value.", | ||
| "float": "The :attribute field must be a float.", | ||
| "gt": { | ||
| "array": "The :attribute field must have more than :value items.", | ||
| "file": "The :attribute field must be greater than :value kilobytes.", | ||
| "numeric": "The :attribute field must be greater than :value.", | ||
| "string": "The :attribute field must be greater than :value characters." | ||
| }, | ||
| "gte": { | ||
| "array": "The :attribute field must have :value items or more.", | ||
| "file": "The :attribute field must be greater than or equal to :value kilobytes.", | ||
| "numeric": "The :attribute field must be greater than or equal to :value.", | ||
| "string": "The :attribute field must be greater than or equal to :value characters." | ||
| }, | ||
| "hex_color": "The :attribute field must be a valid hexadecimal color.", | ||
| "image": "The :attribute field must be an image.", | ||
| "in": "The selected :attribute is invalid.", | ||
| "in_array": "The :attribute field must exist in :other.", | ||
| "in_array_keys": "The :attribute field must contain at least one of the specified keys.", | ||
| "int": "The :attribute field must be an integer.", | ||
| "integer": "The :attribute field must be an integer.", | ||
| "ip": "The :attribute field must be a valid IP address.", | ||
| "ipv4": "The :attribute field must be a valid IPv4 address.", | ||
| "ipv6": "The :attribute field must be a valid IPv6 address.", | ||
| "json": "The :attribute field must be a valid JSON string.", | ||
| "list": "The :attribute field must be a list.", | ||
| "lowercase": "The :attribute field must be lowercase.", | ||
| "lt": { | ||
| "array": "The :attribute field must have less than :value items.", | ||
| "file": "The :attribute field must be less than :value kilobytes.", | ||
| "numeric": "The :attribute field must be less than :value.", | ||
| "string": "The :attribute field must be less than :value characters." | ||
| }, | ||
| "lte": { | ||
| "array": "The :attribute field must not have more than :value items.", | ||
| "file": "The :attribute field must be less than or equal to :value kilobytes.", | ||
| "numeric": "The :attribute field must be less than or equal to :value.", | ||
| "string": "The :attribute field must be less than or equal to :value characters." | ||
| }, | ||
| "mac": "The :attribute field must be a valid MAC address.", | ||
| "mac_address": "The :attribute field must be a valid MAC address.", | ||
| "map": "The :attribute field must be a map.", | ||
| "max": { | ||
| "array": "The :attribute field must not have more than :max items.", | ||
| "file": "The :attribute field must not be greater than :max kilobytes.", | ||
| "numeric": "The :attribute field must not be greater than :max.", | ||
| "string": "The :attribute field must not be greater than :max characters." | ||
| }, | ||
| "max_digits": "The :attribute field must not have more than :max digits.", | ||
| "mimes": "The :attribute field must be a file of type: :values.", | ||
| "mimetypes": "The :attribute field must be a file of type: :values.", | ||
| "min": { | ||
| "array": "The :attribute field must have at least :min items.", | ||
| "file": "The :attribute field must be at least :min kilobytes.", | ||
| "numeric": "The :attribute field must be at least :min.", | ||
| "string": "The :attribute field must be at least :min characters." | ||
| }, | ||
| "min_digits": "The :attribute field must have at least :min digits.", | ||
| "missing": "The :attribute field must be missing.", | ||
| "missing_if": "The :attribute field must be missing when :other is :value.", | ||
| "missing_unless": "The :attribute field must be missing unless :other is :value.", | ||
| "missing_with": "The :attribute field must be missing when :values is present.", | ||
| "missing_with_all": "The :attribute field must be missing when :values are present.", | ||
| "multiple_of": "The :attribute field must be a multiple of :value.", | ||
| "ne": "The :attribute field must not be equal to :value.", | ||
| "not_in": "The selected :attribute is invalid.", | ||
| "not_regex": "The :attribute field format is invalid.", | ||
| "numeric": "The :attribute field must be a number.", | ||
| "present": "The :attribute field must be present.", | ||
| "present_if": "The :attribute field must be present when :other is :value.", | ||
| "present_unless": "The :attribute field must be present unless :other is :value.", | ||
| "present_with": "The :attribute field must be present when :values is present.", | ||
| "present_with_all": "The :attribute field must be present when :values are present.", | ||
| "prohibited": "The :attribute field is prohibited.", | ||
| "prohibited_if": "The :attribute field is prohibited when :other is :value.", | ||
| "prohibited_if_accepted": "The :attribute field is prohibited when :other is accepted.", | ||
| "prohibited_if_declined": "The :attribute field is prohibited when :other is declined.", | ||
| "prohibited_unless": "The :attribute field is prohibited unless :other is in :values.", | ||
| "prohibits": "The :attribute field prohibits :other from being present.", | ||
| "regex": "The :attribute field format is invalid.", | ||
| "required": "The :attribute field is required.", | ||
| "required_array_keys": "The :attribute field must contain entries for: :values.", | ||
| "required_if": "The :attribute field is required when :other is :value.", | ||
| "required_if_accepted": "The :attribute field is required when :other is accepted.", | ||
| "required_if_declined": "The :attribute field is required when :other is declined.", | ||
| "required_unless": "The :attribute field is required unless :other is in :values.", | ||
| "required_with": "The :attribute field is required when :values is present.", | ||
| "required_with_all": "The :attribute field is required when :values are present.", | ||
| "required_without": "The :attribute field is required when :values is not present.", | ||
| "required_without_all": "The :attribute field is required when none of :values are present.", | ||
| "same": "The :attribute field must match :other.", | ||
| "size": { | ||
| "array": "The :attribute field must contain :size items.", | ||
| "file": "The :attribute field must be :size kilobytes.", | ||
| "numeric": "The :attribute field must be :size.", | ||
| "string": "The :attribute field must be :size characters." | ||
| }, | ||
| "slice": "The :attribute field must be a slice.", | ||
| "starts_with": "The :attribute field must start with one of the following: :values.", | ||
| "string": "The :attribute field must be a string.", | ||
| "timezone": "The :attribute field must be a valid timezone.", | ||
| "uint": "The :attribute field must be a positive integer.", | ||
| "ulid": "The :attribute field must be a valid ULID.", | ||
| "unique": "The :attribute has already been taken.", | ||
| "uppercase": "The :attribute field must be uppercase.", | ||
| "url": "The :attribute field must be a valid URL.", | ||
| "uuid": "The :attribute field must be a valid UUID.", | ||
| "uuid3": "The :attribute field must be a valid UUID v3.", | ||
| "uuid4": "The :attribute field must be a valid UUID v4.", | ||
| "uuid5": "The :attribute field must be a valid UUID v5.", | ||
|
|
||
| "len": "The :attribute field must be :size characters.", | ||
| "min_len": "The :attribute field must be at least :min characters.", | ||
| "max_len": "The :attribute field must not be greater than :max characters.", | ||
| "eq_field": "The :attribute field must match :other.", | ||
| "ne_field": "The :attribute field and :other must be different.", | ||
|
h2zi marked this conversation as resolved.
|
||
| "gt_field": "The :attribute field must be greater than :value.", | ||
| "gte_field": "The :attribute field must be greater than or equal to :value.", | ||
| "lt_field": "The :attribute field must be less than :value.", | ||
| "lte_field": "The :attribute field must be less than or equal to :value.", | ||
| "gt_date": "The :attribute field must be a date after :date.", | ||
| "lt_date": "The :attribute field must be a date before :date.", | ||
| "gte_date": "The :attribute field must be a date after or equal to :date.", | ||
| "lte_date": "The :attribute field must be a date before or equal to :date.", | ||
| "number": "The :attribute field must be a number.", | ||
| "full_url": "The :attribute field must be a valid URL." | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.