Sha256: c08c046e92c8410ba35fff055753c18bdb47f8e21b1a8349fe3bf5c277c9253c

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module JSI
  module Schema::Validation::Pattern
    # @private
    def internal_validate_pattern(result_builder)
      if keyword?('pattern')
        value = schema_content['pattern']
        # The value of this keyword MUST be a string.
        if value.respond_to?(:to_str)
          if result_builder.instance.respond_to?(:to_str)
            begin
              # This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression
              # dialect.
              # TODO
              regexp = Regexp.new(value)
              # A string instance is considered valid if the regular expression matches the instance
              # succssfully. Recall: regular expressions are not implicitly anchored.
              result_builder.validate(
                regexp.match(result_builder.instance),
                'instance string does not match `pattern` regular expression value',
                keyword: 'pattern',
              )
            rescue RegexpError => e
              result_builder.schema_error("`pattern` is not a valid regular expression: #{e.message}", 'pattern')
            end
          end
        else
          result_builder.schema_error('`pattern` is not a string', 'pattern')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsi-0.7.0 lib/jsi/schema/validation/pattern.rb