$anchor : String

$anchor

String

This keyword is used to create plain name fragments that are not tied to any particular structural location for referencing purposes, which are taken into consideration for static referencing.

Value This keyword must be set to a string starting with a letter and containing letters, digits, hyphens, underscores, colons, or periods Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
Kind Identifier
Applies To Any
Base Dialect 2020-12
Changed In None
Introduced In 2019-09
Vocabulary Core
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.2.2
Metaschema https://json-schema.org/draft/2020-12/meta/core
Official Tests draft2020-12/anchor.json
Default None
Annotation None
Affected By None
Affects
Also See

The $anchor keyword associates a subschema with the given URI fragment identifier, which can be referenced using the $ref keyword. The fragment identifier is resolved against the URI of the schema resource. Therefore, using this keyword to declare the same anchor more than once within the same schema resource results in an invalid schema.

Examples

A schema that declares a helper schema associated with a location-independent identifier Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/person",
  "properties": {
    "firstName": {
      "$comment": "As a relative reference",
      "$ref": "#internal-string"
    },
    "lastName": {
      "$comment": "As an absolute reference",
      "$ref": "https://example.com/person#internal-string"
    }
  },
  "$defs": {
    "nonEmptyString": {
      "$anchor": "internal-string",
      "type": "string",
      "minLength": 1
    }
  }
}
Valid An object value with non-empty first and last names is valid Instance
{ "firstName": "John", "lastName": "Doe" }
Invalid An object value with empty first and last names is invalid Instance
{ "firstName": "", "lastName": "" }