Sha256: 3ea2909c88d4682b07b6f7e7fa2d3b168e69b7fb2ea792ee11028e366d0eaefb

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Meta
  module JsonSchema
    module Validators
      @validators = {
        validate: proc { |value, p|
          next if value.nil?
          p.call(value)
        },
        required: proc { |value, options, full_options|
          next if options == false

          full_options ||= {}
          options = {} if options == true
          raise JsonSchema::ValidationError, I18n.t(:'json_schema.errors.required') if value.nil?

          if full_options[:type] == 'string' && (!options[:allow_empty]) && value.empty?
            raise JsonSchema::ValidationError, I18n.t(:'json_schema.errors.required')
          end
          if full_options[:type] == 'array' && (options[:allow_empty] == false) && value.empty?
            raise JsonSchema::ValidationError, I18n.t(:'json_schema.errors.required')
          end
        },
        format: proc { |value, format|
          next if value.nil?
          raise JsonSchema::ValidationError, I18n.t(:'json_schema.errors.format') unless value =~ format
        },
        enum: proc { |value, allowable_values|
          next if value.nil?
          raise JsonSchema::ValidationError, I18n.t(:'json_schema.errors.enum', allowable_values: allowable_values) unless allowable_values.include?(value)
        }
      }

      class << self
        def [](key)
          @validators[key]
        end

        def []=(key, validator)
          @validators[key] = validator
        end

        def delete(key)
          @validators.delete(key)
        end

        def keys
          @validators.keys
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meta-api-0.2.0 lib//meta/json_schema/support/validators.rb
meta-api-0.1.2 lib//meta/json_schema/support/validators.rb