What is json schema equivalent of targetNamespace? - json

In any xml file i can say what namespace I refer to using xmlns-attributes to describe the namespace. It is well descibed here: What does "xmlns" in XML mean?
Then I can use a xml schema having a target namespace so that everyone knows that the schema describes that namespace. One question about that is found here: Why do we need targetNamespace?
Using json-schema we can define schemas for json documents. My mental model is that this is roughly equivalent to having a xsd file.
Now, how do I reference the schema in a json object? I can reference a schema using $schema attribute, but how do I declare the name of the schema i develop myself? I dont understand the equivalent of targetNamespace

Researching for writing the question I found the answer. The closest thing of a targetNamespace is the $id attribute. The standard states...
The "$id" keyword defines a URI for the schema, and the base URI that
other URI references within the schema are resolved against. A
subschema's "$id" is resolved against the base URI of its parent
schema. If no parent sets an explicit base with "$id", the base URI is
that of the entire document, as determined per RFC 3986 section 5
[RFC3986].
... which is kind of the mirror image of the leading text for $schema...
The "$schema" keyword is both used as a JSON Schema version identifier
and the location of a resource which is itself a JSON Schema, which
describes any schema written for this particular version. The value
of this keyword MUST be a URI [RFC3986] (containing a scheme) and this
URI MUST be normalized. The current schema MUST be valid against the
meta-schema identified by this URI.
so it is essentially the same thing.
Some things to note, however:
a) you use $schema in a schema to define what schema should be used for defining your own custom schema. It is not stated in the spec that $schema in any kind of object should indicate validation for a schema.
b) You may define in your schema that $schema should be an indication about what schema to use for validation.
c) there are other ways to indicate the schema for data. One such example is using content-type in http headers. Another is to use link http headers.
d) vscode and visual studio both interpret $schema as a reference to a schema for use in validation
The issue has been discussed at the github repo for the spec.
https://github.com/json-schema/json-schema/issues/235
https://github.com/json-schema/json-schema/issues/220#issuecomment-209452992

Related

Given a JSON object with a $schema property, how do I validate it without providing the schema again?

I have an .ndjson file with a bunch of JSON objects. Each object has a $schema property pointing to a file with a JSON Schema defining the validation rules for that object. (If it helps, we can assume that I have a bunch of files instead of a .ndjson file - splitting it into multiple files is easy.)
I've looked around for a way to validate the JSON objects against the referenced $schemas, but all validators I can find require me to specify not only the object but also the schema explicitly.
For example:
jsonschema (Python)
fastjsonschema (Python)
pajv (Node/Commandline)
all require me to specify the schema as input.
Is there a validator that will read the $schema property instead of requiring me to provide the schema explicitly?

Use object keys as type in JSON Schema

Say I want to validate a YAML file against a JSON schema in Intellij IDEA. The file's structure would be like:
foo:
command: touch /tmp/a.txt # I know I don't need this but it's an example
bar:
command: echo "Hello World!" > /tmp/a.txt
baz:
command: cat /tmp/a.txt
dependencies:
- foo
- bar
So the property names can be any string, but the dependencies should only be keys/property names of the root object. Ideally I would specify an enum, but this question suggests it's not possible Use object property keys as enum in JSON schema (unless the answer is obsolete).
Still, I have noticed that when you write a schema in Intellij and you add a "required" = [...] it autocompletes the required fields with the property names of the "property" object (even though it doesn't use them to validate, but close enough for my purpose). I have checked out the schema for it http://json-schema.org/draft-07/schema# but haven't been able to understand how it does that.
Is there a way that I can define my schema so Intellij autocompletes based on another properties' keys like it does when you define a schema?
There is nothing in the schema itself that indicates possible values from data. There's actually no requirement that items in the required array also be defined in properties.
This sort of functionality is defined by the IDE only.
IntelliJ IDEA documents the ability to add custom schemas:
Besides schemas from JSON Schema Store, IntelliJ IDEA lets you
configure and use custom schemas from other storages. You can download
the required schema and store it under the project root or specify the
URL of the resource so IntelliJ IDEA can download the schema
automatically.
To configure a custom JSON Schema:
In the Settings/Preferences dialog ⌘,, go to Languages and Frameworks
| Schemas and DTDs | JSON Schema Mappings.
https://www.jetbrains.com/help/idea/json.html#ws_json_schema_add_custom
It also details later how to make the intelesense provide a rich preview:
Using HTML descriptions in JSON schema #
By default, IntelliJ IDEA escapes HTML characters when displaying
documentation for JSON schema definitions in documentation popups. To
get nice looking documentation with rich HTML markup, store the HTML
description in the x-intellij-html-description extension property
instead of description.
https://www.jetbrains.com/help/idea/json.html#ws_json_show_doc_in_html
However,
autocompletes based on another properties' keys
sounds like custom functionality specifically designed for writing JSON Schema. JSON Schema itself cannot reference data dynamically like that (which I assume is what you were thinking).

Generate Angular2 forms from Swagger API specification

I'm looking for a way to generate a set of Angular2 form templates from a Swagger API definition file. I want a result that will allow me to test my POST/PUT requests, and even use it in my app.
After some research I found this Angular2 form library that takes a JSON schema as input: https://github.com/makinacorpus/angular2-schema-form
So if you know of a Swagger -> JSON Schema converter that will work too.
Cheers!
So if you know of a Swagger -> JSON Schema converter that will work
too.
Swagger 2.0 supports a subset of JSON schema draft 4. This is what swagger's Schema object is. From the docs:
The following properties are taken directly from the JSON Schema
definition and follow the same specifications:
$ref - As a JSON Reference
format (See Data Type Formats for further details)
title
description (GFM syntax can be used for rich text representation)
default (Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object)
multipleOf
...
The following properties are taken from the JSON Schema definition but
their definitions were adjusted to the Swagger Specification.
items
allOf
properties
additionalProperties
It should be a fairly simple exercise to manually extract the schema from your swagger, but I don't know of any automated tool to do this. I think the fact some of the JSON schema properties have been modified by swagger may make auto conversion problematic in certain circumstances.

How do I validate JSON against XML Schema (XSD) or RelaxNG?

I'm defining the structure of JSON documents. I'd like to know if how to validate the JSON documents against existing XSD, RelaxNG schemas or another standard schema language schema. I know of Jsonix, but I don't see that it uses the regular expressions from XSD or RelaxNG for validation against JSON schema (and I don't think that JSON schema is standardized).
Clarification: We already have existing XML and XSD. We can always go back to XML for validation, but it would be cool if we could validate the JSON directly, and would give us more confidence when we want to use the JSON and not XML.
Update: Here is the specification in question: http://www.web3d.org/specifications/x3d-3.4.xsd note that it doesn't have text nodes in the XML documents.
Preliminary answer (still a work in progress, but you can contribute):
If you want to convert XML schema to JSON schema, try downloading: XSD2OWL stylesheet which converts XML Schema to OWL. I converted my schema to owl like this:
$ xmlsh
$ xslt -f xsd2owl.xsl -cf file.xsd > file.owl
$ exit
Then download owl2jsonschema.js ** NO LICENSE ** and modify it until it until the demo works. The output will be in demo/OUTPUT/schema folder/*.json as separate JSON files.
XSD and RelaxNG are defined against XML, not JSON.
For JSON, see JSON Schema, but realize that it has nowhere near the adoption of XSD, and the latest draft of the specification expired August 3, 2013, casting doubts on the future of the effort.
Update
How do I validate JSON against XML Schema (XSD) or RelaxNG?
You don't.
The question is not "Can I?" but "How?" Say I have total control over
the JSON document.
When the answer to "Can I?" is "No" the question of how does not apply.
Clarification: We already have existing XML and XSD. We can always go
back to XML for validation, but it would be cool if we could validate
the JSON directly, and would give us more confidence when we want to
use the JSON and not XML.
You can validate the JSON directly against a JSON Schema, but not against an XSD. There are no tools that can do that; the standards are substantially different. The need to define standard vocabularies and grammars that is served by XSD and RelaxNG against XML was intended to be met by JSON Schema against JSON.
You're looking for "confidence when we want to use the JSON and not XML" in the wrong place. See reasons for choosing XML vs JSON instead.
As you're probably already aware of the information I'm about to post, this is just for reference.
Jsonix Schema Compiler supports generation of JSON Schema based on the XML Schema.
So with this you can convert you XML Schema into JSON Schema and validate your JSON against this JSON Schema using AJV.
This is still an experimental feature but that's the direction.

Is it possible to use invalid(non existing) Uri for JSON schema definition?

Is it possible to use invalid(non existing) Uri for JSON schema definition?
So that I can specify it and use for versioning, without need to deploy it anywhere?
A URL is expected to resolve to the resource, so if you say "this is the URL for the schema" then that URL should resolve to the schema.
However, URLs are not the only sort of URI - it sounds like a URN might be what you want. In contrast to a URL (uniform resource location), a URN (uniform resource name) is an identifier for a resource, but it doesn't carry a generic method to resolve it.
For example, the URN urn:ietf:rfc:2648 is an identifier for RFC 2648, but there isn't a standard way to get the RFC text from just that URN (you'd need some kind of special service that knew about urn:ietf:rfc:... URNs). If you used something like this, then it should (in theory) do what you want.
(You might run into trouble referencing one schema from another if your library is mistakenly assuming all URIs are URLs, but that would be a bug in your library.)