Class: Senko::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/senko/validator.rb

Defined Under Namespace

Classes: EvaluationContext, NoopEvaluationContext

Constant Summary collapse

MESSAGES =
{
  false_schema: 'boolean schema false does not allow any value',
  type: 'expected %{expected}, got %{actual}',
  required: "missing required property '%{property}'",
  minimum: 'value must be >= %{minimum}',
  exclusive_minimum: 'value must be > %{minimum}',
  maximum: 'value must be <= %{maximum}',
  exclusive_maximum: 'value must be < %{maximum}',
  min_length: 'string length must be >= %{min_length}',
  max_length: 'string length must be <= %{max_length}',
  pattern: "string does not match pattern '%{pattern}'",
  min_items: 'array length must be >= %{min_items}',
  max_items: 'array length must be <= %{max_items}',
  unique_items: 'array items must be unique',
  min_contains: 'array must contain at least %{min_contains} matching item(s)',
  max_contains: 'array must contain at most %{max_contains} matching item(s)',
  min_properties: 'object must have >= %{min_properties} properties',
  max_properties: 'object must have <= %{max_properties} properties',
  enum: 'value must be one of: %{values}',
  const: 'value must be %{const}',
  multiple_of: 'value must be a multiple of %{multiple_of}',
  format: "value does not match format '%{format}'",
  not: 'value should not match the schema',
  one_of_none: 'value must match exactly one schema in oneOf (matched none)',
  one_of_multiple: 'value must match exactly one schema in oneOf (matched %{count})',
  any_of: 'value must match at least one schema in anyOf',
  discriminator: "value does not match discriminator property '%{property}'",
  additional_properties: "unexpected property '%{property}'",
  property_names: "property name '%{property}' is invalid",
  dependent_required: "property '%{property}' requires property '%{dependency}'",
  unevaluated_properties: "unevaluated property '%{property}'",
  unevaluated_items: 'unevaluated item at index %{index}',
  custom_keyword: "value failed custom keyword '%{keyword}'"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Validator

Returns a new instance of Validator.



220
221
222
223
224
225
# File 'lib/senko/validator.rb', line 220

def initialize(options = {})
  @options = options
  @custom_formats = options[:custom_formats] || {}
  @messages = MESSAGES.merge(symbolized_messages(options[:messages] || {}))
  @tracking_cache = {}
end

Class Method Details

.requires_evaluation_tracking?(instructions, seen = {}) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
180
181
182
183
# File 'lib/senko/validator.rb', line 177

def requires_evaluation_tracking?(instructions, seen = {})
  return false unless instructions.is_a?(Array)
  return false if seen[instructions.object_id]

  seen[instructions.object_id] = true
  instructions.any? { |instruction| instruction_requires_evaluation_tracking?(instruction, seen) }
end

Instance Method Details

#valid?(instructions, data, track_evaluation: self.class.requires_evaluation_tracking?(instructions)) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
236
# File 'lib/senko/validator.rb', line 233

def valid?(instructions, data, track_evaluation: self.class.requires_evaluation_tracking?(instructions))
  context = track_evaluation ? EvaluationContext.new : NoopEvaluationContext.new
  valid_instructions?(instructions, data, '', context)
end

#validate(instructions, data, fail_fast: false) ⇒ Object



227
228
229
230
231
# File 'lib/senko/validator.rb', line 227

def validate(instructions, data, fail_fast: false)
  result = Result.new(fail_fast: fail_fast)
  validate_instructions(instructions, data, result, '', EvaluationContext.new)
  result
end