module Esquema::KeywordValidator
def self.validate!(property_name, type, options) # rubocop:disable Metrics/AbcSize
-
(ArgumentError)
- If the type is unknown. -
(ArgumentError)
- If the property type is not a symbol. -
(ArgumentError)
- If the property name is not a symbol. -
(ArgumentError)
- If the options are not in the VALID_OPTIONS constant.
Options Hash:
(**options)
-
:const
(Object
) -- The constant value for the property. -
:enum
(Array
) -- The allowed values for the property. -
:default
(Object
) -- The default value for the property.
Parameters:
-
options
(Hash
) -- The options for the property. -
type
(Symbol
) -- The type of the property. -
property_name
(Symbol
) -- The name of the property being validated.
def self.validate!(property_name, type, options) # rubocop:disable Metrics/AbcSize options.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, options[:default]) if options.key?(:default) validate_enum(property_name, type, options[:enum]) if options.key?(:enum) validate_const(property_name, type, options[:const]) if options.key?(:const) end
def self.validate_const(property_name, type, const)
-
const
(Object
) -- The constant value for the property. -
type
(Symbol
) -- The type of the property. -
property_name
(Symbol
) -- The name of the property being validated.
def self.validate_const(property_name, type, const) validate_value!(property_name, type, const, "const") end
def self.validate_default(property_name, type, default)
-
default
(Object
) -- The default value for the property. -
type
(Symbol
) -- The type of the property. -
property_name
(Symbol
) -- The name of the property being validated.
def self.validate_default(property_name, type, default) validate_value!(property_name, type, default, "default") end
def self.validate_enum(property_name, type, enum)
-
(ArgumentError)
- If the enum is not an array.
Parameters:
-
enum
(Array
) -- The allowed values for the property. -
type
(Symbol
) -- The type of the property. -
property_name
(Symbol
) -- The name of the property being validated.
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
def self.validate_value!(property_name, type, value, keyword)
-
(ArgumentError)
- If the value does not match the type.
Parameters:
-
keyword
(String
) -- The keyword being validated (e.g., "default", "enum"). -
value
(Object
) -- The value to be validated. -
type
(Symbol
) -- The type of the property. -
property_name
(Symbol
) -- The name of the property being validated.
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