Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors.full_messages returning the full json schema config #65

Open
DavidGeismarLtd opened this issue Dec 13, 2022 · 3 comments
Open

errors.full_messages returning the full json schema config #65

DavidGeismarLtd opened this issue Dec 13, 2022 · 3 comments

Comments

@DavidGeismarLtd
Copy link

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": {}
        }]
    ]
}
@remi
Copy link
Member

remi commented Dec 13, 2022

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!

@DavidGeismarLtd
Copy link
Author

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

@remi
Copy link
Member

remi commented Dec 13, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants