2

According to JSON-Schema 7.0 String section, I can use a "relative-json-pointer" to make sure that a property value is an exact match of a parent key.

In the examples section (5.1) of Relative JSON Pointers, it shows that "going up one level" and get the key value, is "0#".

Given the following JSON document:

{
    "valid": {
        "name": "valid"
    },
    "invalid": {
        "name": "invalid, because this value is not the same value as the parent key"
    }
}

The following json-schema should catch the "invalid" object:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": {
        "type": "object",
        "required": ["name"],
        "properties": {
            "name": {
                "type": "string",
                "json-pointer": "0#"
            }
        }
    }
}

I am sure the json-schema is sound, since it will catch the two objects, "invalid2" and "invalid3", in the following JSON document:

{
    "valid": {
        "name": "valid"
    },
    "invalid": {
        "name": "invalid, because this value is not the same value as the parent key"
    },
    "invalid2": {   },
    "invalid3": { "name": 2 }
}

I have used ajv-cli 3.3.0 and python jsonschema 3.2.0 to test with. But neither validation implementations will catch the "invalid" object. Both implementations claim to fully support JSON Schema draft 7.

Community
  • 1
  • 1
dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54

1 Answers1

1

I can use a "relative-json-pointer" to make sure that a property value is an exact match of a parent key.

I'm not sure how you're reading that section, but that's not what it says at all.

That section you reference, on the documentation site, is listing a set of built-in possible values for the format keyword.

It does nothing WITH the value you want to validate, for example, a relative-json-pointer.

What is it you WANT to do?

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • You are right that it is related to `format`. I will try that. But it does say "Note that this should be used only when the entire string contains only JSON Pointer content" for `"json-pointer"`. I assume the same is true for `"relative-json-pointer"` in the same section. – dotnetCarpenter Nov 06 '20 at 14:54
  • Yes. It notes that the WHOLE string must be a JSON pointer, such as `/foo/bar` as opposed to a uri-reference such as `#/foo/bar/`. – Relequestual Nov 06 '20 at 14:56
  • 1
    The json-pointer that you're validating with `format: json-pointer` is in the instance. The pointer isn't in the schema. There is no `json-pointer` keyword. – Relequestual Nov 06 '20 at 14:58
  • Well, since it's a built-in I suppose that I can not use Relative JSON Pointers in the schema but only if they were in the JSON document. To answer your question: I want to catch the `"invalid"` object as an error, since its `"name"` property does not equal its object key. – dotnetCarpenter Nov 06 '20 at 14:58
  • I see. No, you can't do that in JSON Schema. Schemas cannot reference locations in the instance. – Relequestual Nov 06 '20 at 15:00
  • I have looked at "`$ref"` and `"$id"` but fail to see how I can set the required value of the `name` property with those... – dotnetCarpenter Nov 06 '20 at 15:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224205/discussion-between-dotnetcarpenter-and-relequestual). – dotnetCarpenter Nov 06 '20 at 15:03