-
Notifications
You must be signed in to change notification settings - Fork 0
Add format: presets, which export the JSON Schema format keyword #20
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
Merged
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -116,9 +116,12 @@ def nullify!(schema, field) | |
|
|
||
| def scalar_schema(field) | ||
| schema = SCALAR_SCHEMAS.fetch(field[:type]).dup | ||
| apply_format_name!(schema, field) | ||
| apply_in!(schema, field[:in]) | ||
| apply_string_bounds!(schema, field) | ||
| apply_pattern!(schema, field[:format]) | ||
| # A preset's pattern is authored by this gem rather than by the app, so | ||
| # it needs no heuristic — see apply_pattern!. | ||
| apply_pattern!(schema, field[:format], vouched: !field[:format_name].nil?) | ||
| schema | ||
| end | ||
|
|
||
|
|
@@ -135,6 +138,16 @@ def opaque_schema(field) | |
| schema | ||
| end | ||
|
|
||
| # A `format:` preset also names the JSON Schema `format` keyword the | ||
| # ecosystem understands, which a hand-written Regexp cannot. `pattern` is | ||
| # still emitted next to it: in draft 2020-12 `format` is an annotation | ||
| # unless a validator opts into asserting it, so the pattern is what | ||
| # actually enforces. | ||
| def apply_format_name!(schema, field) | ||
| json = FORMATS.dig(field[:format_name], :json) | ||
| schema["format"] = json if json | ||
| end | ||
|
|
||
| def array_schema(field, unknown:) | ||
| schema = { "type" => "array" } | ||
| min, max = length_bounds(field[:length]) | ||
|
|
@@ -173,10 +186,16 @@ def apply_string_bounds!(schema, field) | |
| schema["maxLength"] = max if max | ||
| end | ||
|
|
||
| def apply_pattern!(schema, regexp) | ||
| # `vouched:` marks a pattern this gem authored (a `format:` preset), which | ||
| # is known translatable and so skips the conservative scan. It has to: | ||
| # the RFC-derived :email pattern contains `*+` inside a character class, | ||
| # which UNTRANSLATABLE reads — deliberately over-eagerly — as a | ||
| # possessive quantifier, and the most common format in Rails would | ||
| # otherwise publish no pattern at all. | ||
| def apply_pattern!(schema, regexp, vouched: false) | ||
|
Owner
Author
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. The |
||
| return unless regexp | ||
|
|
||
| pattern = ecma_pattern(regexp) | ||
| pattern = ecma_pattern(regexp, vouched: vouched) | ||
| if pattern | ||
| schema["pattern"] = pattern | ||
| else | ||
|
|
@@ -188,11 +207,11 @@ def apply_pattern!(schema, regexp) | |
| # Flagged regexps bail entirely (JSON Schema's `pattern` has no flag | ||
| # slot, and /x//m/i all change semantics), as does any source containing | ||
| # an untranslatable construct. | ||
| def ecma_pattern(regexp) | ||
| def ecma_pattern(regexp, vouched: false) | ||
| return nil unless regexp.options.zero? | ||
|
|
||
| source = regexp.source | ||
| return nil if source.match?(UNTRANSLATABLE) | ||
| return nil if !vouched && source.match?(UNTRANSLATABLE) | ||
|
|
||
| source.gsub('\A', "^").gsub('\z', "$") | ||
| end | ||
|
|
||
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
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.
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.
Using
URI::MailTo::EMAIL_REGEXPunder the hood for:emailmaintains exact parity with the regexp Rails developers commonly use, avoiding unexpected validation divergences.