Sha256: bf2a6f5f41d8e00fca8e805f76fc257155de48be147e64afe9327fcca2cfa7e9
Contents?: true
Size: 1003 Bytes
Versions: 1
Compression:
Stored size: 1003 Bytes
Contents
module GovukSchemas class Validator attr_reader :schema_name, :type, :payload def initialize(schema_name, type, payload) @schema_name = schema_name @type = type @payload = 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.to_json) validator.map { |message| "- " + humanized_error(message) }.join("\n") end def formatted_payload return payload if payload.is_a?(String) JSON.pretty_generate(payload) end def humanized_error(message) message.gsub("The property '#/'", "The item") .gsub(/in schema [0-9a-f\-]+/, "") .strip end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
govuk_schemas-4.4.0 | lib/govuk_schemas/validator.rb |