The compiler selects a JSON Schema dialect from the schema resource's $schema
URI. When $schema is absent, Draft 2020-12 is used by default.
compiler := jsonschema.NewCompiler()
compiler.SetDefaultDialect(jsonschema.Draft4)
schema, err := compiler.Compile(schemaBytes)Supported dialects:
| Dialect | Constant |
|---|---|
| Draft 2020-12 | jsonschema.Draft202012 |
| Draft 2019-09 | jsonschema.Draft201909 |
| Draft-07 | jsonschema.Draft7 |
| Draft-06 | jsonschema.Draft6 |
| Draft-04 | jsonschema.Draft4 |
Each schema resource carries its selected dialect, and nested resources can
switch dialect when they declare their own $schema.
if schema.Dialect() == jsonschema.Draft4 {
// handle legacy schema source if needed
}Compatibility behavior is normalized during compilation:
definitionsis compiled as$defs.- Legacy tuple
items: [...]plusadditionalItemsis compiled to the internal tuple model. - Legacy
dependenciesis compiled todependentRequiredordependentSchemas. - Draft-04 boolean
exclusiveMinimumandexclusiveMaximumare compiled to numeric exclusive bounds. - Draft-04
idis used as the schema identifier. - Draft-07, Draft-06, and Draft-04
$refignore sibling keywords.
format remains annotation-only unless Compiler.SetAssertFormat(true) is
enabled. Compile does not perform schema meta-validation by default; call
ValidateSchema when the schema document itself is untrusted.
result, err := compiler.ValidateSchema(schemaBytes)
if err != nil {
return err
}
if !result.IsValid() {
return fmt.Errorf("invalid schema document: %v", result.Errors)
}Draft-04, Draft-06, and Draft-07 meta-schemas are available without a loader. Draft 2019-09, Draft 2020-12, and custom meta-schemas use the compiler's registered schema cache and loaders.