Sha256: 1005a140cbd3eff18afacd3513e791a88f98fc8fdf28d6a0d71ab2ad2644734e
Contents?: true
Size: 1.07 KB
Versions: 1
Compression:
Stored size: 1.07 KB
Contents
module GovukSchemas class Validator attr_reader :schema_name, :type attr_accessor :payload def initialize(schema_name, type, payload) @schema_name = schema_name @type = type @payload = ensure_json(payload) end def valid? errors.empty? end def error_message <<~DOC expected the payload to be valid against the '#{schema_name}' schema: #{formatted_payload} Validation errors: #{errors} DOC end private def errors schema = Schema.find("#{type}_schema": schema_name) validator = JSON::Validator.fully_validate(schema, payload) validator.map { |message| "- " + humanized_error(message) }.join("\n") end def formatted_payload JSON.pretty_generate(JSON.parse(payload)) end def humanized_error(message) message.gsub("The property '#/'", "The item") .gsub(/in schema [0-9a-f\-]+/, "") .strip end def ensure_json(payload) return payload if payload.is_a?(String) payload.to_json end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
govuk_schemas-4.4.1 | lib/govuk_schemas/validator.rb |