davishmcclurg / json_schemer

JSON Schema validator. Supports drafts 4, 6, 7, 2019-09, 2020-12, OpenAPI 3.0, and OpenAPI 3.1.
MIT License
399 stars 64 forks source link

Touble parsing key with forward slashes #185

Closed natebrunette closed 3 months ago

natebrunette commented 3 months ago

I'm trying to use an openapi document and I'm having trouble parsing a path directly using document.ref("#/paths//my/path/..."), I believe because it contains forward slashes. I've tried escaping the slashses, but still doesn't work. Currently, I'm grabbing @document directly and traversing through the structure directly, but I'd rather use .ref.

davishmcclurg commented 3 months ago

I've tried escaping the slashses, but still doesn't work. Currently, I'm grabbing @document directly and traversing through the structure directly, but I'd rather use .ref.

How did you escape them? If you replace them with ~1, ref should be able to find them correctly. For example:


schemer = JSONSchemer.schema({
    "$defs": {
        "x": {
            "$defs": {
                "y/o": {
                    "$defs": {
                        "z": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
})

subschemer = schemer.ref("#/$defs/x/$defs/y~1o/$defs/z")
subschemer.valid?(1)
>> false
natebrunette commented 3 months ago

Ah, I didn't know about ~1. It's working now! Is that a ruby, json schema, or json_schemer thing?

davishmcclurg commented 3 months ago

Ah, I didn't know about ~1. It's working now! Is that a ruby, json schema, or json_schemer thing?

Haha none of the above! It's from JSON pointer (which JSON schema uses): https://datatracker.ietf.org/doc/html/rfc6901#section-3

Glad it's working.