activerecord_json_validator icon indicating copy to clipboard operation
activerecord_json_validator copied to clipboard

errors.full_messages returning the full json schema config

Open DavidGeismarLtd opened this issue 3 years ago • 3 comments

I m using the gem to validation the jsonb field config in my model RegistrationForm :

class RegistrationForm < ApplicationRecord
  CONFIG_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'registration_forms', 'config.json')
  validates :config, presence: true, json: { message: ->(errors) { errors }, schema: CONFIG_JSON_SCHEMA } # `schema: :dynamic_profile_schema` would also work
end

It works well. However as mentioned in the README, you should be able to render errors in a human-readable format array by specifying a proc as the value of the message key.

However this is what I am getting back running registration_form.errors.full_messages on an invalid registration_form : registration_form.errors.full_messages

["Config {\"data\"=>{\"name\"=>\"name\", \"label\"=>\"random label\"}, \"data_pointer\"=>\"/0\", \"schema\"=>{\"type\"=>\"object\", \"default\"=>{}, \"title\"=>\"Schema for item\", \"required\"=>[\"label\", \"kind\", \"name\"], \"properties\"=>{\"label\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The label Schema\", \"minLength\"=>1, \"examples\"=>[\"My label\"]}, \"kind\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The kind Schema\", \"enum\"=>[\"text\", \"number\", \"select\"], \"examples\"=>[\"text\"]}, \"name\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The name Schema\", \"minLength\"=>1, \"examples\"=>[\"My name\"]}, \"value\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The value Schema\", \"examples\"=>[\"My value\"]}, \"options\"=>{\"type\"=>\"object\", \"default\"=>{}, \"title\"=>\"The options Schema\", \"required\"=>[], \"properties\"=>{}, \"examples\"=>[{}]}}, \"examples\"=>[{\"label\"=>\"My label\", \"kind\"=>\"text\", \"name\"=>\"My name\", \"value\"=>\"My value\", \"options\"=>{}}]}, \"schema_pointer\"=>\"/items\", \"root_schema\"=>{\"type\"=>\"array\", \"default\"=>[], \"title\"=>\"Root Schema\", \"items\"=>{\"type\"=>\"object\", \"default\"=>{}, \"title\"=>\"Schema for item\", \"required\"=>[\"label\", \"kind\", \"name\"], \"properties\"=>{\"label\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The label Schema\", \"minLength\"=>1, \"examples\"=>[\"My label\"]}, \"kind\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The kind Schema\", \"enum\"=>[\"text\", \"number\", \"select\"], \"examples\"=>[\"text\"]}, \"name\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The name Schema\", \"minLength\"=>1, \"examples\"=>[\"My name\"]}, \"value\"=>{\"type\"=>\"string\", \"default\"=>\"\", \"title\"=>\"The value Schema\", \"examples\"=>[\"My value\"]}, \"options\"=>{\"type\"=>\"object\", \"default\"=>{}, \"title\"=>\"The options Schema\", \"required\"=>[], \"properties\"=>{}, \"examples\"=>[{}]}}, \"examples\"=>[{\"label\"=>\"My label\", \"kind\"=>\"text\", \"name\"=>\"My name\", \"value\"=>\"My value\", \"options\"=>{}}]}, \"examples\"=>[[{\"label\"=>\"My label\", \"kind\"=>\"text\", \"name\"=>\"My name\", \"value\"=>\"My value\", \"options\"=>{}}]], \"$id\"=>\"file:///Users/davidgeismar/code/360_ecommerce_solution/360-saas-multitenant/config/schemas/registration_forms/config.json\"}, \"type\"=>\"required\", \"details\"=>{\"missing_keys\"=>[\"kind\"]}}"]

This seems quite far from the human readable messages in the README.

What is going on here ?

My json schema (type array) :

{
    "type": "array",
    "default": [],
    "title": "Root Schema",
    "items": {
        "type": "object",
        "default": {},
        "title": "Schema for item",
        "required": [
            "label",
            "kind",
            "name"
        ],
        "properties": {
            "label": {
                "type": "string",
                "default": "",
                "title": "The label Schema",
                "minLength": 1,
                "examples": [
                    "My label"
                ]
            },
            "kind": {
                "type": "string",
                "default": "",
                "title": "The kind Schema",
                "enum": ["text", "number", "select"],
                "examples": [
                    "text"
                ]
            },
            "name": {
                "type": "string",
                "default": "",
                "title": "The name Schema",
                "minLength": 1,
                "examples": [
                    "My name"
                ]
            },
            "value": {
                "type": "string",
                "default": "",
                "title": "The value Schema",
                "examples": [
                    "My value"
                ]
            },
            "options": {
                "type": "object",
                "default": {},
                "title": "The options Schema",
                "required": [],
                "properties": {},
                "examples": [{}]
            }
        },
        "examples": [{
            "label": "My label",
            "kind": "text",
            "name": "My name",
            "value": "My value",
            "options": {}
        }]
    },
    "examples": [
        [{
            "label": "My label",
            "kind": "text",
            "name": "My name",
            "value": "My value",
            "options": {}
        }]
    ]
}

DavidGeismarLtd avatar Dec 13 '22 13:12 DavidGeismarLtd

Hi @DavidGeismarLtd,

Can you provide the exact version of the gem and the json-schema gem you are using?

The output of this command would be useful to pinpoint the problem:

$ cat Gemfile.lock | grep json

Thanks!

remi avatar Dec 13 '22 13:12 remi

Hi @remi : activerecord_json_validator (2.0.1) json_schemer (~> 0.2.18)

DavidGeismarLtd avatar Dec 13 '22 14:12 DavidGeismarLtd

I was able to reproduce the problem with these exact versions:

"Data {\"data\"=>{}, \"data_pointer\"=>\"\", \"schema\"=>{\"$schema\"=>\"http://json-schema.org/draft-04/schema#\", \"type\"=>\"object\", \"properties\"=>{\"action\"=>{\"type\"=>\"string\", \"pattern\"=>\"^(ok|kill|alert)$\"}, \"message\"=>{\"type\"=>\"string\"}, \"buttons\"=>{\"type\"=>\"array\", \"items\"=>{\"type\"=>\"object\", \"properties\"=>{\"type\"=>{\"type\"=>\"string\", \"pattern\"=>\"^(url|cancel|reload)$\"}, \"label\"=>{\"type\"=>\"string\"}, \"url\"=>{\"type\"=>\"string\"}, \"order\"=>{\"type\"=>\"integer\"}}, \"required\"=>[\"type\", \"label\"]}}}, \"required\"=>[\"action\"], \"id\"=>\"file:///Users/remi/Code/mirego/killswitch/config/schemas/behavior_data.jsonschema\"}, \"schema_pointer\"=>\"\", \"root_schema\"=>{\"$schema\"=>\"http://json-schema.org/draft-04/schema#\", \"type\"=>\"object\", \"properties\"=>{\"action\"=>{\"type\"=>\"string\", \"pattern\"=>\"^(ok|kill|alert)$\"}, \"message\"=>{\"type\"=>\"string\"}, \"buttons\"=>{\"type\"=>\"array\", \"items\"=>{\"type\"=>\"object\", \"properties\"=>{\"type\"=>{\"type\"=>\"string\", \"pattern\"=>\"^(url|cancel|reload)$\"}, \"label\"=>{\"type\"=>\"string\"}, \"url\"=>{\"type\"=>\"string\"}, \"order\"=>{\"type\"=>\"integer\"}}, \"required\"=>[\"type\", \"label\"]}}}, \"required\"=>[\"action\"], \"id\"=>\"file:///Users/remi/Code/mirego/killswitch/config/schemas/behavior_data.jsonschema\"}, \"type\"=>\"required\", \"details\"=>{\"missing_keys\"=>[\"action\"]}}"

However, when using activerecord_json_validator 2.1.3, I get this output:

"Data root is missing required keys: action"

I think you should upgrade to activerecord_json_validator 2.1.3 🙂

Let me know if you’re able to reproduce the bug then!

remi avatar Dec 13 '22 14:12 remi