Class: HecksPlugins::JSONValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/hecks-plugins-json-validator.rb,
lib/matchers.rb,
lib/parsers/schema_parser.rb,
lib/parsers/message_parser.rb

Overview

A validator that is built on top of json-schema to provide more functionality than the basic hecks validator. Builds a json schema from a head spec and validates the user’s input against it.

Defined Under Namespace

Classes: MessageParser, SchemaParser

Constant Summary collapse

MATCHERS =

These matchers create a cleaner, more user-friendly message out of the messages returned by json-schema

[
  {
    regex:   /did not contain a required property of '(.*)' in schema/,
    message: 'Missing'
  },
  {
    regex: /The property '#\/(.*)' of type (.*) did not match the following type: (.*) in schema/,
    message: 'Type mismatch. Got \'%s\', should have been \'%s\''
  }
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:) ⇒ JSONValidator

Returns a new instance of JSONValidator.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hecks-plugins-json-validator.rb', line 13

def initialize(command:)
  @args          = command.args
  @domain_module = command.domain_module
  @errors        = {}
  @schema_parser = SchemaParser.new(
    domain_module: domain_module,
    object:        command.domain_module.head
  ).call

  @validator     = JSON::Validator
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/hecks-plugins-json-validator.rb', line 11

def errors
  @errors
end

Instance Method Details

#callObject



25
26
27
28
29
# File 'lib/hecks-plugins-json-validator.rb', line 25

def call
  parse_schema
  validate
  self
end