w3c / wot-thing-description

Web of Things (WoT) Thing Description
http://w3c.github.io/wot-thing-description/
Other
131 stars 63 forks source link

Specifying a @context in json-schema #1233

Open ioggstream opened 3 years ago

ioggstream commented 3 years ago

Question

The following definition https://www.w3.org/2019/wot/json-schema#example-5-schema-with-context-annotation to be a valid json-schema needs probably to register @context and jsonld:context as json-schema keywords.

{
    "@context": {
        ...,
        "jsonld": "http://www.w3.org/ns/json-ld#"
    }
    "jsonld:context": "http://schema.org",
    "type": "object",
    "description": "Schema of a commercial product with GTIN and manufacturer",
    "properties" : {
        "gtin14": { "type": "string" },
        "manufacturer": { "type": "string" }
    }
}

A cleaner way to do it could be to just express the context in a given property (in yaml syntax).

type: object
description: "Schema of a commercial product with GTIN and manufacturer"
properties:
  gtin: { "type": "string" }
  manufacturer: { "type": "string" },
  "@context":
    const:
      "@vocab": http://schema.org/

cc: @relu91

egekorkan commented 2 years ago

I am not sure if I understand this correctly, the goal is not to say that this whole thing is a JSON Schema but that you can extend a JSON Schema with these annotations. @context and jsonld:context are not (validation) keywords to be picked up by a JSON Schema parser. The cleaner way is wrong in my point of view. The actual clean way would be to treat them as JSON Schema vocabulary items that are introduced in the recent JSON Schema version

ioggstream commented 2 years ago

Hi @egekorkan! The goal is to extend json-schema with annotations referencing a jsonld context.

In Example 6 we see a json-ld object

{
    "@context": "http://schema.org",
    "name": "some manufactured product",
    "gtin14": "0 00 12345 60001 2",
    "manufacturer": "http://comapny.example.com"
}

where the @context property is not defined in the above json-schema.

Since a json-schema best practice suggests to use additionalProperties: false and add a list of required fields (for example, the above json schema validates almost any object, including {"user": "foo", "password": "bar" }.

This means that, in practice, if you need to validate Example6 you will probably need to add the @context property thus making it a validation keyword (but you may don't want to have such a strict validation though).

type: object
description: "Schema of a commercial product with GTIN and manufacturer"
additionalProperties: false
required:
- gtin
- manufacturer
properties:
  gtin: { "type": "string" }
  manufacturer: { "type": "string" },
  "@context":
    const:
      "@vocab": http://schema.org/

About Vocabulary Items, they are probably a suitable solution: I'll have a look!

ioggstream commented 2 years ago

@egekorkan Are you thinking to something like this? https://gist.githubusercontent.com/ioggstream/8e858509a3ca535c5af230986aeefaf7/raw/jsonld-context-metaschema.json

url = 'https://gist.githubusercontent.com/ioggstream/8e858509a3ca535c5af230986aeefaf7/raw/jsonld-context-metaschema.json'
schema = requests.get(url).json()
validate(instance={"jsonld:context": "https://schema.org", "type": "string", "format": "uri"}, schema=schema)

# Ok

validate(instance=
{"jsonld:context": {"@vocab": "https://schema.org"},
 "type": "string", 
 "format": "uri"
}, schema=schema)
# Ok

validate(instance=
{"jsonld:context": 1, # expected string or object, not integer
"type": "string", 
"format": "uri"
}, schema=schema)
1
# KO
egekorkan commented 2 years ago

Yes exactly. This is a nice proposal by the way and my previous comment is not correct after understanding the actual issue. Not sure how to move forwards though

ioggstream commented 2 years ago

@egekorkan I think there are a lot of use cases here... If you manage to get in touch with the document editors please drop me a line :)

egekorkan commented 2 years ago

@ioggstream editors should be watching this but I think that @vcharpenay is the person who actually wrote this part. In the end, do you want this vocabulary mechanism mentioned informatively (like a guideline/tip) somewhere or are you looking for a solution?

P.S. Sorry I had forgotten about this issue

ioggstream commented 2 years ago

I have now a proposal to attach a jsonld context to a jsonschema. The proposal, with a schema design UI is described here https://docs.google.com/document/d/1fBRH2wtg1p_g4voNSTlHiSJmKgvfNaIsUXwPBO36RuM/edit?usp=drivesdk

OTOH, I also like to understand if the specs are ok or not

ioggstream commented 2 years ago

Hi folks, I better formalized the proposal here https://www.ietf.org/archive/id/draft-polli-restapi-ld-keywords-00.html

iiuc there's not a reliable way to map a JSON Schema document to RDF using the context defined in this spec: is that correct?

Related PR here https://github.com/ioggstream/draft-polli-restapi-ld-keywords/pull/3