Module: Esquema::KeywordValidator
- Defined in:
- lib/esquema/keyword_validator.rb
Overview
The KeywordValidator module provides functionality for validating schema keyword values. There are three types of keyword values that must be validated against the type of the property they are associated with:
-
default: The default value for a property.
-
enum: The allowed values for a property.
-
const: The constant value for a property.
Constant Summary collapse
- VALID_OPTIONS =
The valid options for a property.
%i[type title description maxLength minLength pattern maxItems minItems maxProperties minProperties properties additionalProperties dependencies enum format multipleOf maximum exclusiveMaximum minimum exclusiveMinimum const allOf anyOf oneOf not default items uniqueItems virtual].freeze
- TYPE_VALIDATORS =
Hash containing type validators for different data types.
{ string: ->(value) { value.is_a?(String) }, integer: ->(value) { value.is_a?(Integer) }, number: ->(value) { value.is_a?(Numeric) }, boolean: ->(value) { [true, false].include?(value) }, array: ->(value) { value.is_a?(Array) }, object: ->(value) { value.is_a?(Hash) }, null: ->(value) { value.nil? }, date: ->(value) { value.is_a?(Date) }, datetime: ->(value) { value.is_a?(DateTime) }, time: ->(value) { value.is_a?(Time) } }.freeze
Class Method Summary collapse
-
.validate!(property_name, type, options) ⇒ Object
Validates a property based on its type and options.
-
.validate_const(property_name, type, const) ⇒ Object
Validates the constant value for a property.
-
.validate_default(property_name, type, default) ⇒ Object
Validates the default value for a property.
-
.validate_enum(property_name, type, enum) ⇒ Object
Validates the allowed values for a property.
-
.validate_value!(property_name, type, value, keyword) ⇒ Object
Validates a value based on its type and keyword.
Class Method Details
.validate!(property_name, type, options) ⇒ Object
Validates a property based on its type and options.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/esquema/keyword_validator.rb', line 43 def self.validate!(property_name, type, ) # rubocop:disable Metrics/AbcSize .assert_valid_keys(VALID_OPTIONS) raise ArgumentError, "Property must be a symbol" unless property_name.is_a?(Symbol) raise ArgumentError, "Property type must be a symbol" unless type.is_a?(Symbol) raise ArgumentError, "Unknown type #{type}" unless TYPE_VALIDATORS.key?(type) validate_default(property_name, type, [:default]) if .key?(:default) validate_enum(property_name, type, [:enum]) if .key?(:enum) validate_const(property_name, type, [:const]) if .key?(:const) end |
.validate_const(property_name, type, const) ⇒ Object
Validates the constant value for a property.
80 81 82 |
# File 'lib/esquema/keyword_validator.rb', line 80 def self.validate_const(property_name, type, const) validate_value!(property_name, type, const, "const") end |
.validate_default(property_name, type, default) ⇒ Object
Validates the default value for a property.
59 60 61 |
# File 'lib/esquema/keyword_validator.rb', line 59 def self.validate_default(property_name, type, default) validate_value!(property_name, type, default, "default") end |
.validate_enum(property_name, type, enum) ⇒ Object
Validates the allowed values for a property.
69 70 71 72 73 |
# File 'lib/esquema/keyword_validator.rb', line 69 def self.validate_enum(property_name, type, enum) raise ArgumentError, "Enum for #{property_name} is not an array" unless enum.is_a?(Array) enum.each { |value| validate_value!(property_name, type, value, "enum") } end |
.validate_value!(property_name, type, value, keyword) ⇒ Object
Validates a value based on its type and keyword.
91 92 93 94 95 96 |
# File 'lib/esquema/keyword_validator.rb', line 91 def self.validate_value!(property_name, type, value, keyword) validator = TYPE_VALIDATORS[type] return if validator.call(value) raise ArgumentError, "#{keyword.capitalize} value for #{property_name} does not match type #{type}" end |