propertyNames : Schema
propertyNames
SchemaValidation succeeds if the schema validates against every property name in the instance.
Value | This keyword must be set to a valid JSON Schema |
---|---|
Kind | Applicator |
Applies To | Object |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 6 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.2.4 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/propertyNames.json |
Default |
{}
|
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The propertyNames
keyword in is used to define constraints on the property names within an object instance. It allows you to specify a schema that all the property names in an object instance must adhere to.
Common Pitfall
Note: Note that the property names in any object instance will always be strings. Therefore, this schema only makes sense when applied to strings. Passing a schema here that matches something other than a string would be invalid.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"propertyNames": { "maxLength": 3 }
}
{ "foo": "foo", "bar": 33 }
{ "name": "John Doe", "age": 21 }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"propertyNames": true
}
{ "foo": "bar" }
[ "no", "effect" ]
propertyNames
has no effect on instances other than objects.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"propertyNames": { "type": "number" }
}
{ "foo": 22 }
{}
- The property names in any object instance cannot be a number. Therefore, any object instance will fail against the above schema, except for an empty object.