问题描述:

I have a JSON object:

{

"name" : "car",

"color" : "blue"

}

And a JSON Schema which is used to validate it (not specified here).

XML allows you to include a reference to an extern XML Schema Definition for validation in the XML document itself via "xsi:schemaLocation" (the namespace prefix might be different).

Is there an equivalent way to include a reference to a JSON Schema in a JSON object?

网友答案:

The link from data to a schema describing it is generally considered "metadata", so usually isn't in the data itself.

If you're working over HTTP, you can:

  • use a "profile=" parameter in the Content-Type header, or
  • use a "Link:" header, with rel="describedby" to link to the schema.

The second option is preferable.

If you're loading from a file, or something else, then it's not defined. Within your own application, you are perfectly free to define something like "$schema" to reference schemas. Although other tools wouldn't pick up on this automatically, it would be obvious to any other developers what you were doing.

相关阅读:
Top