json-schema-core(liuhailong翻译)

来源:互联网 发布:vm安装linux虚拟机 编辑:程序博客网 时间:2024/06/05 03:23

JSON Schema: 核心定义和术语(json-schema-core)


互联网工程任务组(IETF)
互联网草案
预期状态:Informational
文档到期时间:2013年8月3日
作者:fge. Galiegue、K. Zyp, Ed. SitePen (美国加州某地)、G. Court
文档提交时间:2013年1月30日
翻译:liuhailong,http://blog.csdn.net/stationxp


摘要

JSON Schema定义了的新的媒体类型(media type) :”application/schema+json”,是一个以JSON为格式表述的,用于定JSON数据的结构。JSON Schema描述了针对特定应用的JSON数据的格式应该是什么样的,以及如何与之交互。JSON Schema定义了JSON数据的验证、文档化、链接和导航,交互控制方式。

文档状态

This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet-Drafts is at http://datatracker.ietf.org/drafts/current/.

Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as “work in progress.”

这个文档草案将于2013年8月3日过期。

版权提示

Copyright (c) 2013 IETF Trust and the persons identified as the document authors. All rights reserved.

This document is subject to BCP 78 and the IETF Trust’s Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.

目录

  1. 简介
  2. 术语与约定
  3. 核心术语
    3.1. 属性和元素
    3.2. JSON Schema, 关键词
    3.3. 空schema
    3.4. Root schema, subschema
    3.5. JSON Schema原始类型
    3.6. JSON 值相等的判断依据
    3.7. 实例
  4. 简介
    4.1. 验证
    4.2. 超媒体和链接
  5. 总体原则
    5.1. 兼容所有的JSON值
    5.2. 独立于编程语言
    5.3. JSON Schema和HTTP
    5.4. JSON Schema和其他协议
    5.5. 整数
    5.6. 扩展SON Schema
    5.7. 安全考虑
  6. “$schema” 关键字
    6.1. 目的
    6.2. 自定义
  7. 使用URI区分命名空间
    7.1. 定义
    7.2. 使用id关键字定义命名空间
    7.2.1. 有效值
    7.2.2. 用法
    7.2.3. 规范的取值和内联引用
    7.2.4. 内联引用和片段
    7.3. 安全考虑
  8. 使用HTTP协议时的注意事项
    8.1. “Content-Type”请求头信息
    8.2. “Link”头信息
  9. IANA
  10. 引用
    10.1. 规范化引用
    10.2. 信息引用
    附录 A. 变更日志

1. 简介

JSON Schema定义了的新的媒体类型(media type) :”application/schema+json”,是一个以JSON为格式表述的,用于定JSON数据的结构。JSON Schema描述了针对特定应用的JSON数据的格式应该是什么样的,以及如何与之交互。JSON Schema定义了JSON数据的验证、文档化、链接和导航,交互控制方式。

这份规范定义了JSON Schema核心术语和机制;其他的相关规范将在这个规范基础上定义JSON Schema的不同应用。

2. 术语与约定

The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in RFC 2119 [RFC2119].
关键词“必须”、“不得”、“必须”、“应当”、“不得”、“应该”、“不应该”, “推荐”、“可能”、“可选”参见RFC 2119[ http://tools.ietf.org/html/rfc2119 ]。

术语“JSON”、“JSON文本”、“JSON值”、“member”、“element”, “object”、”array”、”number”、”string”、 “boolean”、”true”、 “false”和 “null”参见RFC 4627[ http://tools.ietf.org/html/rfc4627 ]。

3. 核心术语

3.1. 属性Property, 元素item

依照rfc4627,提到JSON对象时,“member”和“property”是等价的。后面都称为“属性“。

依照rfc4627,提到JSON数组时,“element”和“item”是等价的。后面都称为“元素“。

3.2. JSON Schema, keywords

JSON Schema本身是一个JSON文档, 并且这个文档必须是一个对象(object)。
由当前规范定义(或相关规范)的JSON Schema对象的属性称为关键字(或者称为schema关键字)。

一个JSON Schema中可以存在不是关键字的属性。

3.3. 空模式

无属性,或者只包含不是关键字属性的JSON Schema称为空模式。
一个空的图式是一种无属性的JSON图式,或性能的 不是模式关键词。

3.4. 根模式, 子模式

下面演示了一个没有子模式的JSON Schema

{    "title": "root"}

JSON Schemas 可以嵌套,如下所示。

{    "title": "root",    "otherSchema": {        "title": "nested",        "anotherSchema": {            "title": "alsoNested"        }    }}

在这个例子中”nested” and “alsoNested” 都是子模式,”root” 是根模式。

3.5. JSON Schema支持的原始类型

JSON Schema定义了7种原始类型:

  1. array
  2. boolean
  3. integer
  4. number(包括任何数字,包括整数)
  5. null
  6. object
  7. string

3.6. JSON值相等的判断依据

Two JSON values are said to be equal if and only if:

both are nulls; or

both are booleans, and have the same value; or

both are strings, and have the same value; or

both are numbers, and have the same mathematical value; or

both are arrays, and:

have the same number of items; and

items at the same index are equal according to this definition; or

both are objects, and:

have the same set of property names; and

values for a same property name are equal according to this definition.

3.7. 实例instance

相对于JSON Schema来说,实例(instance)指的是具体的JSON数据,可以时任何JSON值。或者称为JSON实例或JSON数据。

4. 概述

This document proposes a new media type “application/schema+json” to identify JSON Schema for describing JSON data. JSON Schemas are themselves written in JSON. This, and related specifications, define keywords allowing to describe this data in terms of allowable values, textual descriptions and interpreting relations with other resources. The following sections are a summary of features defined by related specifications.

4.1. 校验

JSON Schema allows applications to validate instances, either non interactively or interactively. For instance, an application may collect JSON data and check that this data matches a given set of constraints; another application may use a JSON Schema to build an interactive interface in order to collect user input according to constraints described by JSON Schema.

4.2. 超媒体和链接

JSON Schema provides a method for extracting link relations from instances to other resources, as well as describing interpretations of instances as multimedia data. This allows JSON data to be interpreted as rich hypermedia documents, placed in the context of a larger set of related resources.

5. 总体原则

5.1. Applicability to all JSON values

It is acknowledged that an instance may be any valid JSON value as defined by [RFC4627]. As such, JSON Schema does not mandate that an instance be of a particular type: JSON Schema can describe any JSON value, including null.

5.2. Programming language independence

JSON Schema is programming language agnostic. The only limitations are the ones expressed by [RFC4627] and those of the host programming language.

5.3. JSON Schema and HTTP

This specification acknowledges the role of HTTP [RFC2616] as the dominant protocol in use on the Internet, and the wealth of official specifications related to it.

This specification uses a subset of these specifications to recommend a set of mechanisms, usable by this protocol, to associate JSON instances to one or more schemas.

5.4. JSON Schema and other protocols

JSON Schema does not define any semantics for the client-server interface for any other protocols than HTTP. These semantics are application dependent, or subject to agreement between the parties involved in the use of JSON Schema for their own needs.

5.5. Mathematical integers

It is acknowledged by this specification that some programming languages, and their associated parsers, use different internal representations for floating point numbers and integers, while others do not.

As a consequence, for interoperability reasons, JSON values used in the context of JSON Schema, whether that JSON be a JSON Schema or an instance, SHOULD ensure that mathematical integers be represented as integers as defined by this specification.

5.6. Extending JSON Schema

Implementations MAY choose to define additional keywords to JSON Schema. Save for explicit agreement, schema authors SHALL NOT expect these additional keywords to be supported by peer implementations. Implementations SHOULD ignore keywords they do not support.

5.7. Security considerations

Both schemas and instances are JSON values. As such, all security considerations defined in RFC 4627 [RFC4627] apply.

6. The “$schema” keyword

6.1. Purpose

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.

This keyword MUST be located at the root of a JSON Schema. The value of this keyword MUST be a URI [RFC3986] and a valid JSON Reference [json‑reference]; this URI MUST be both absolute and normalized. The resource located at this URI MUST successfully describe itself. It is RECOMMENDED that schema authors include this keyword in their schemas.

The following values are predefined:

http://json-schema.org/schema#
JSON Schema written against the current version of the specification.
http://json-schema.org/hyper-schema#
JSON Schema written against the current version of the specification.
http://json-schema.org/draft-04/schema#
JSON Schema written against this version.
http://json-schema.org/draft-04/hyper-schema#
JSON Schema hyperschema written against this version.
http://json-schema.org/draft-03/schema#
JSON Schema written against JSON Schema, draft v3 [json‑schema‑03].
http://json-schema.org/draft-03/hyper-schema#
JSON Schema hyperschema written against JSON Schema, draft v3 [json‑schema‑03].

6.2. Customization

When extending JSON Schema with custom keywords, schema authors SHOULD define a custom URI for “$schema”. This custom URI MUST NOT be one of the predefined values.

7. URI resolution scopes and dereferencing

7.1. Definition

JSON Schema uses JSON Reference [json‑reference] as a mechanism for schema addressing. It extends this specification in two ways:

JSON Schema offers facilities to alter the base URI against which a reference must resolve by the means of the “id” keyword;

it defines a specific dereferencing mechanism extending JSON Reference to accept arbitrary fragment parts.

Altering the URI within a schema is called defining a new resolution scope. The initial resolution scope of a schema is the URI of the schema itself, if any, or the empty URI if the schema was not loaded from a URI.

7.2. URI resolution scope alteration with the “id” keyword

7.2.1. Valid values

The value for this keyword MUST be a string, and MUST be a valid URI. This URI MUST be normalized, and SHOULD NOT be an empty fragment (#) or the empty URI.

7.2.2. Usage

The “id” keyword (or “id”, for short) is used to alter the resolution scope. When an id is encountered, an implementation MUST resolve this id against the most immediate parent scope. The resolved URI will be the new resolution scope for this subschema and all its children, until another id is encountered.

When using “id” to alter resolution scopes, schema authors SHOULD ensure that resolution scopes are unique within the schema.

This schema will be taken as an example:

{    "id": "http://x.y.z/rootschema.json#",    "schema1": {        "id": "#foo"    },    "schema2": {        "id": "otherschema.json",        "nested": {            "id": "#bar"        },        "alsonested": {            "id": "t/inner.json#a"        }    },    "schema3": {        "id": "some://where.else/completely#"    }}

Subschemas at the following URI-encoded JSON Pointer [json‑pointer]s (starting from the root schema) define the following resolution scopes:

# (document root)
http://x.y.z/rootschema.json#

#/schema1
http://x.y.z/rootschema.json#foo

#/schema2
http://x.y.z/otherschema.json#

#/schema2/nested
http://x.y.z/otherschema.json#bar

#/schema2/alsonested
http://x.y.z/t/inner.json#a

#/schema3
some://where.else/completely#

7.2.3. Canonical dereferencing and inline dereferencing

When resolving a URI against a resolution scope, an implementation may choose two modes of operation:

canonical dereferencing
The implementation dereferences all resolved URIs.
inline dereferencing
The implementation chooses to dereference URIs within the schema.
Implementations MUST support canonical dereferencing, and MAY support inline dereferencing.

For example, consider this schema:

{    "id": "http://my.site/myschema#",    "definitions": {        "schema1": {            "id": "schema1",            "type": "integer"        },        "schema2", {            "type": "array",            "items": { "$ref": "schema1" }        }    }}

When an implementation encounters the “schema1” reference, it resolves it against the most immediate parent scope, leading to URI “http://my.site/schema1#“. The way to process this URI will differ according to the chosen dereferencing mode:

if canonical dereferencing is used, the implementation will dereference this URI, and fetch the content at this URI;

if inline dereferencing is used, the implementation will notice that URI scope “http://my.site/schema1#” is already defined within the schema, and choose to use the appropriate subschema.

7.2.4. Inline dereferencing and fragments

When using inline dereferencing, a resolution scope may lead to a URI which has a non empty fragment part which is not a JSON Pointer, as in this example:

{    "id": "http://some.site/schema#",    "not": { "$ref": "#inner" },    "definitions": {        "schema1": {            "id": "#inner",            "type": "boolean"        }    }}

An implementation choosing to support inline dereferencing SHOULD be able to use this kind of reference. Implementations choosing to use canonical dereferencing, however, are not required to support it.

7.3. Security considerations

Inline dereferencing can produce canonical URIs which differ from the canonical URI of the root schema. Schema authors SHOULD ensure that implementations using canonical dereferencing obtain the same content as implementations using inline dereferencing.

Extended JSON References using fragments which are not JSON Pointers are not dereferenceable by implementations choosing not to support inline dereferencing. This kind of reference is defined for backwards compatibility, and SHOULD NOT be used in new schemas.

It is acknowledged by this specification that the majority of interactive JSON Schema processing will be over HTTP. This section therefore gives recommendations for materializing an instance/schema correlation using mechanisms currently available for this protocol. An instance is said to be described by one (or more) schema(s).

TOC
8.1. Correlation by means of the “Content-Type” header

It is RECOMMENDED that a MIME type parameter by the name of “profile” be appended to the “Content-Type” header of the instance being processed. If present, the value of this parameter MUST be a valid URI, and this URI SHOULD resolve to a valid JSON Schema. The MIME type MUST be “application/json”, or any other subtype.

An example of such a header would be:

Content-Type: application/my-media-type+json;
profile=http://example.com/my-hyper-schema#

TOC
8.2. Correlation by means of the “Link” header

When using the “Link” header, the relation type used MUST be “describedBy”, as defined by RFC 5988, section 5.3 [RFC5988]. The target URI of the “Link” header MUST be a valid JSON Schema.

An example of such a header would be:

Link: http://example.com/my-hyper-schema#; rel=”describedBy”

9. IANA Considerations

The proposed MIME media type for JSON Schema is defined as follows:

type name: application;

subtype name: schema+json.

10. References

10.1. Normative References

[RFC2119] Bradner, S., “Key words for use in RFCs to Indicate Requirement Levels,” BCP 14, RFC 2119, March 1997 (TXT, HTML, XML).

10.2. Informative References

[RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H., Masinter, L., Leach, P., and T. Berners-Lee, “Hypertext Transfer Protocol – HTTP/1.1,” RFC 2616, June 1999 (TXT, PS, PDF, HTML, XML).
[RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, “Uniform Resource Identifier (URI): Generic Syntax,” STD 66, RFC 3986, January 2005 (TXT, HTML, XML).
[RFC4627] Crockford, D., “The application/json Media Type for JavaScript Object Notation (JSON),” RFC 4627, July 2006 (TXT).
[RFC5988] Nottingham, M., “Web Linking,” RFC 5988, October 2010 (TXT).
[json-reference] Bryan, P. and K. Zyp, “JSON Reference (work in progress),” September 2012.
[json-pointer] Bryan, P. and K. Zyp, “JSON Pointer (work in progress),” September 2012.
[json-schema-03] Court, G. and K. Zyp, “JSON Schema, draft 3,” September 2012.

Appendix A. ChangeLog

draft-00
Initial draft.
Salvaged from draft v3.
Mandate the use of JSON Reference, JSON Pointer.
Define the role of “id”. Define URI resolution scope.
Add interoperability considerations.

Authors’ Addresses

作者:

Francis Galiegue
EMail: fgaliegue@gmail.com

编辑:

Kris Zyp (editor)
SitePen (USA)
530 Lytton Avenue
Palo Alto, CA 94301
USA
Phone: +1 650 968 8787
EMail: kris@sitepen.com

作者?

Gary Court
Calgary, AB
Canada
EMail: gary.court@gmail.com

翻译

liuhailong
Beijing,China
EMail:liuhailong2008#foxmail.com

0 0
原创粉丝点击