8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/zod_rails/mapping/regexp_mapper.rb', line 8
def self.call(regex)
if unsupported?(regex)
ZodRails.logger.warn("ZodRails: Skipping regexp with Ruby-only semantics: #{regex.inspect}")
return nil
end
pattern = regex.source
.gsub("\\A", "^")
.gsub("\\z", "(?![\\s\\S])")
.gsub("\\Z", "(?=(?:\\n)?(?![\\s\\S]))")
flags = flags_for(regex)
args = [JSON.generate(pattern), (JSON.generate(flags) unless flags.empty?)].compact
"new RegExp(#{args.join(", ")})"
end
|