Movatterモバイル変換


[0]ホーム

URL:


Reference

object

Using non-strings as keys is invalid JSON:

Properties

The properties (key-value pairs) on an object are defined using thepropertieskeyword. The value ofproperties is an object, where eachkey is the name of a property and each value is aschema used tovalidate that property. Any property that doesn't match any of theproperty names in theproperties keyword is ignored by this keyword.

SeeAdditional Properties andUnevaluated Properties for how to disallow properties thatdon't match any of the property names inproperties.

For example, let's say we want to define a simple schema for an addressmade up of a number, street name and street type:

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"number":{"type":"number"},"street_name":{"type":"string"},"street_type":{"enum":["Street","Avenue","Boulevard"]}}}
Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue"}
Checkmark iconcompliant to schema

// If we provide the number in the wrong type, it is invalid:

Copy iconCopied icon
data
{"number":"1600","street_name":"Pennsylvania","street_type":"Avenue"}
Error iconnot compliant to schema

By default, leaving out properties is valid. SeeRequired Properties.

Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania"}
Checkmark iconcompliant to schema

By extension, even an empty object is valid:

Copy iconCopied icon
data
{}
Checkmark iconcompliant to schema

By default, providing additional properties is valid:

Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue","direction":"NW"}
Checkmark iconcompliant to schema

Pattern Properties

Sometimes you want to say that, given a particular kind of propertyname, the value should match a particular schema. That's wherepatternProperties comes in: it maps regular expressions to schemas. Ifa property name matches the given regular expression, the property valuemust validate against the corresponding schema.

Regular expressions are not anchored. This means that when defining theregular expressions forpatternProperties, it's important to notethat the expression may match anywhere within the property name. Forexample, the regular expression"p" will match any property name withap in it, such as"apple", not just a property whose name is simply"p". It's therefore usually less confusing to surround the regularexpression in^...$, for example,"^p$".

In this example, any properties whose names start with the prefixS_must be strings, and any with the prefixI_ must be integers. Anyproperties that do not match either regular expression are ignored.

Copy iconCopied icon
 logo-white schema
{"type":"object","patternProperties":{"^S_":{"type":"string"},"^I_":{"type":"integer"}}}
Copy iconCopied icon
data
{"S_25":"This is a string"}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"I_0":42}
Checkmark iconcompliant to schema

If the name starts withS_, it must be a string

Copy iconCopied icon
data
{"S_0":42}
Error iconnot compliant to schema

If the name starts withI_, it must be an integer

Copy iconCopied icon
data
{"I_42":"This is a string"}
Error iconnot compliant to schema

This is a key that doesn't match any of the regular expressions:

Copy iconCopied icon
data
{"keyword":"value"}
Checkmark iconcompliant to schema

Additional Properties

TheadditionalProperties keyword is used to control the handling ofextra stuff, that is, properties whose names are not listed in theproperties keyword or match any of the regular expressions in thepatternProperties keyword. By default any additional properties areallowed.

The value of theadditionalProperties keyword is a schema that will beused to validate any properties in theinstance that are not matched byproperties orpatternProperties. Setting theadditionalPropertiesschema tofalse means no additional properties will be allowed.

Reusing the example fromProperties, but this time settingadditionalProperties tofalse.

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"number":{"type":"number"},"street_name":{"type":"string"},"street_type":{"enum":["Street","Avenue","Boulevard"]}},"additionalProperties":false}
Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue"}
Checkmark iconcompliant to schema

SinceadditionalProperties isfalse, this extra property "direction" makes the object invalid:

Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue","direction":"NW"}
Error iconnot compliant to schema

You can use non-boolean schemas to put more complex constraints on theadditional properties of an instance. For example, one can allowadditional properties, but only if their values are each a string:

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"number":{"type":"number"},"street_name":{"type":"string"},"street_type":{"enum":["Street","Avenue","Boulevard"]}},"additionalProperties":{"type":"string"}}
Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue"}
Checkmark iconcompliant to schema

This is valid, since the additional property's value is a string:

Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue","direction":"NW"}
Checkmark iconcompliant to schema

This is invalid, since the additional property's value is not a string:

Copy iconCopied icon
data
{"number":1600,"street_name":"Pennsylvania","street_type":"Avenue","office_number":201}
Error iconnot compliant to schema

You can useadditionalProperties with a combination ofpropertiesandpatternProperties. In the following example, based on the examplefrompatternProperties, we add a"builtin" property,which must be a number, and declare that all additional properties (thatare neither defined byproperties nor matched bypatternProperties)must be strings:

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"builtin":{"type":"number"}},"patternProperties":{"^S_":{"type":"string"},"^I_":{"type":"integer"}},"additionalProperties":{"type":"string"}}
Copy iconCopied icon
data
{"builtin":42}
Checkmark iconcompliant to schema

This is a key that doesn't match any of the regular expressions:

Copy iconCopied icon
data
{"keyword":"value"}
Checkmark iconcompliant to schema

It must be a string:

Copy iconCopied icon
data
{"keyword":42}
Error iconnot compliant to schema

Extending Closed Schemas

It's important to note thatadditionalProperties only recognizesproperties declared in the samesubschema as itself. So,additionalProperties can restrict you from "extending" a schemausingcombining keywords such asallOf. Inthe following example, we can see how theadditionalProperties cancause attempts to extend the address schema example to fail.

Copy iconCopied icon
 logo-white schema
{"allOf":[{"type":"object","properties":{"street_address":{"type":"string"},"city":{"type":"string"},"state":{"type":"string"}},"required":["street_address","city","state"],"additionalProperties":false}],
"properties":{"type":{"enum":["residential","business"]}},"required":["type"]}

FailsadditionalProperties. "type" is considered additional.

Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business"}
Error iconnot compliant to schema

Failsrequired. "type" is required.

Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC"}
Error iconnot compliant to schema

BecauseadditionalProperties only recognizes properties declared inthe same subschema, it considers anything other than"street_address", "city", and "state" to be additional. Combiningthe schemas withallOf doesn't change that. A workaroundyou can use is to moveadditionalProperties to the extending schemaand redeclare the properties from the extended schema.

Copy iconCopied icon
 logo-white schema
{"allOf":[{"type":"object","properties":{"street_address":{"type":"string"},"city":{"type":"string"},"state":{"type":"string"}},"required":["street_address","city","state"]}],
"properties":{"street_address":true,"city":true,"state":true,"type":{"enum":["residential","business"]}},"required":["type"],"additionalProperties":false}
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business"}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business","something that doesn't belong":"hi!"}
Error iconnot compliant to schema

Now theadditionalProperties keyword is able to recognize all thenecessary properties and the schema works as expected. Keep reading tosee how theunevaluatedProperties keyword solves this problem withoutneeding to redeclare properties.

Unevaluated Properties

starNew in draft 2019-09

In the previous section we saw the challenges with usingadditionalProperties when "extending" a schema usingcombining. TheunevaluatedProperties keyword is similartoadditionalProperties except that it can recognize propertiesdeclared in subschemas. So, the example from the previous section can berewritten without the need to redeclare properties.

Copy iconCopied icon
 logo-white schema
{"allOf":[{"type":"object","properties":{"street_address":{"type":"string"},"city":{"type":"string"},"state":{"type":"string"}},"required":["street_address","city","state"]}],
"properties":{"type":{"enum":["residential","business"]}},"required":["type"],"unevaluatedProperties":false}
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business"}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business","something that doesn't belong":"hi!"}
Error iconnot compliant to schema

unevaluatedProperties works by collecting any properties that aresuccessfully validated when processing the schemas and using those asthe allowed list of properties. This allows you to do more complexthings like conditionally adding properties. The following exampleallows the "department" property only if the "type" of address is"business".

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"street_address":{"type":"string"},"city":{"type":"string"},"state":{"type":"string"},"type":{"enum":["residential","business"]}},"required":["street_address","city","state","type"],
"if":{"type":"object","properties":{"type":{"const":"business"}},"required":["type"]},"then":{"properties":{"department":{"type":"string"}}},
"unevaluatedProperties":false}
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"business","department":"HR"}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"street_address":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","type":"residential","department":"HR"}
Error iconnot compliant to schema

In this schema, the properties declared in thethen schema only countas "evaluated" properties if the "type" of the address is"business".

Required Properties

By default, the properties defined by theproperties keyword are notrequired. However, one can provide a list of required properties usingtherequired keyword.

Therequired keyword takes an array of zero or more strings. Each ofthese strings must be unique.

Draft-specific info
info blue
In Draft 4,required must contain at least one string.

In the following example schema defining a user record, we require thateach user has a name and e-mail address, but we don't mind if theydon't provide their address or telephone number:

Copy iconCopied icon
 logo-white schema
{"type":"object","properties":{"name":{"type":"string"},"email":{"type":"string"},"address":{"type":"string"},"telephone":{"type":"string"}},"required":["name","email"]}
Copy iconCopied icon
data
{"name":"William Shakespeare","email":"[email protected]"}
Checkmark iconcompliant to schema

Providing extra properties is fine, even properties not defined in the schema:

Copy iconCopied icon
data
{"name":"William Shakespeare","email":"[email protected]","address":"Henley Street, Stratford-upon-Avon, Warwickshire, England","authorship":"in question"}
Checkmark iconcompliant to schema

Missing the required "email" property makes the JSON document invalid:

Copy iconCopied icon
data
{"name":"William Shakespeare","address":"Henley Street, Stratford-upon-Avon, Warwickshire, England",}
Error iconnot compliant to schema

In JSON a property with valuenull is not equivalent to the property not being present. This fails becausenull is not of type "string", it's of type "null"

Copy iconCopied icon
data
{"name":"William Shakespeare","address":"Henley Street, Stratford-upon-Avon, Warwickshire, England","email":null}
Error iconnot compliant to schema

Property names

starNew in draft 6

The names of properties can be validated against a schema, irrespectiveof their values. This can be useful if you don't want to enforcespecific properties, but you want to make sure that the names of thoseproperties follow a specific convention. You might, for example, want toenforce that all names are valid ASCII tokens so they can be used asattributes in a particular programming language.

Copy iconCopied icon
 logo-white schema
{"type":"object","propertyNames":{"pattern":"^[A-Za-z_][A-Za-z0-9_]*$"}}
Copy iconCopied icon
data
{"_a_proper_token_001":"value"}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"001 invalid":"value"}
Error iconnot compliant to schema

Since object keys must always be strings anyway, it is implied that theschema given topropertyNames is always at least:

Copy iconCopied icon
 logo-white schema
{"type":"string"}

Size

The number of properties on an object can be restricted using theminProperties andmaxProperties keywords. Each of these must be anon-negative integer.

Copy iconCopied icon
 logo-white schema
{"type":"object","minProperties":2,"maxProperties":3}
Copy iconCopied icon
data
{}
Error iconnot compliant to schema
Copy iconCopied icon
data
{"a":0}
Error iconnot compliant to schema
Copy iconCopied icon
data
{"a":0,"b":1}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"a":0,"b":1,"c":2}
Checkmark iconcompliant to schema
Copy iconCopied icon
data
{"a":0,"b":1,"c":2,"d":3}
Error iconnot compliant to schema

Need Help?

Did you find these docs helpful?

Help us make our docs great!

At JSON Schema, we value docs contributions as much as every other type of contribution!

Still Need Help?

Learning JSON Schema is often confusing, but don't worry, we are here to help!.


[8]ページ先頭

©2009-2025 Movatter.jp