Module: Gitlab::GrapeOpenapi::Concerns::RegexConverter
- Included in:
- Gitlab::GrapeOpenapi::Converters::ParameterConverter, Models::RequestBody::ParameterSchema
- Defined in:
- lib/gitlab/grape_openapi/concerns/regex_converter.rb
Overview
Converts Ruby Regexp objects into ECMA-262 (JavaScript) compatible pattern strings for use in OpenAPI ‘pattern` schema fields.
OpenAPI requires ‘pattern` values to be valid ECMA-262 regular expressions, but Ruby RegExes routinely contain constructs that ECMA-262 cannot parse: A / z anchors, inline option groups like (?i-mx:…), POSIX classes, etc. js_regex translates these into JS-compatible equivalents.
The Ruby /i flag is folded into the pattern itself (via character class expansion) so the resulting pattern carries no flags, which OpenAPI’s flag-less ‘pattern` field requires
Constant Summary collapse
- JS_REGEX_TARGET =
js_regex’s ‘target:` controls which ECMAScript version’s regex features it will emit. ES2018 is the newest target the gem supports (as of 3.14.0); earlier targets like the default (ES5) silently drop lookbehinds, changing the semantics of any Ruby regex that relies on them
'ES2018'
Instance Method Summary collapse
Instance Method Details
#regexp_to_pattern(value) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/gitlab/grape_openapi/concerns/regex_converter.rb', line 27 def regexp_to_pattern(value) regexp = extract_regexp(value) return unless regexp JsRegex.new(inline_case_insensitivity(regexp), target: JS_REGEX_TARGET).source end |