Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ List<Error> errors = schema.validate(input, InputFormat.JSON, executionContext -
});
```

### Require schema dialect to be specified

The specification allows for the `$schema` keyword not to be specified, in which case the schema will default to the default dialect specified.

The following example creates a `SchemaRegistry` that does not specify a default dialect and will throw a `MissingSchemaKeywordException` if the schema does not specify a dialect using the `$schema` keyword.

```java
SchemaRegistry registry = SchemaRegistry.builder().dialectRegistry(new BasicDialectRegistry(Dialects.getDraft202012())).build();
```

### Results and output formats

#### Results
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.schema;

/**
* Thrown when the $schema keyword is expected as no default dialect id was
* specified.
*/
public class MissingSchemaKeywordException extends SchemaException {
private static final long serialVersionUID = 1L;

public MissingSchemaKeywordException(String message) {
super(message);
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/networknt/schema/SchemaRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ public SchemaRegistry build() {

private SchemaRegistry(NodeReader nodeReader, String defaultDialectId, SchemaLoader schemaLoader,
boolean schemaCacheEnabled, DialectRegistry dialectRegistry, SchemaRegistryConfig schemaRegistryConfig) {
if (defaultDialectId == null || defaultDialectId.trim().isEmpty()) {
throw new IllegalArgumentException("defaultDialectId must not be null or empty");
}
this.nodeReader = nodeReader != null ? nodeReader : BasicNodeReader.getInstance();
this.defaultDialectId = defaultDialectId;
this.schemaLoader = schemaLoader != null ? schemaLoader : SchemaLoader.getDefault();
Expand Down Expand Up @@ -549,6 +546,10 @@ private Dialect getDialectOrDefault(final JsonNode schemaNode) {
throw new SchemaException("Unknown dialect: " + iriNode);
}
final String iri = iriNode == null || iriNode.isNull() ? defaultDialectId : iriNode.textValue();
if (iri == null) {
throw new MissingSchemaKeywordException(
"The $schema keyword that indicates the schema dialect must be specified.");
}
return getDialect(iri);
}

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/networknt/schema/SchemaRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import org.junit.jupiter.api.Test;

import com.networknt.schema.dialect.BasicDialectRegistry;
import com.networknt.schema.dialect.Dialect;
import com.networknt.schema.dialect.Dialects;

/**
* Tests for JsonSchemaFactory.
Expand Down Expand Up @@ -87,4 +89,31 @@ public void run() {
});
assertFalse(failed.get());
}

@Test
void noDefaultDialect() {
SchemaRegistry registry = SchemaRegistry.builder()
.dialectRegistry(new BasicDialectRegistry(Dialects.getDraft202012())).build();
assertThrows(MissingSchemaKeywordException.class, () -> {
registry.getSchema("{\"type\":\"object\"}");
});
}

@Test
void noDefaultDialectButSchemaSpecified() {
SchemaRegistry registry = SchemaRegistry.builder()
.dialectRegistry(new BasicDialectRegistry(Dialects.getDraft202012())).build();
assertDoesNotThrow(() -> {
registry.getSchema("{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"type\":\"object\"}");
});
}

@Test
void noDefaultDialectButSchemaSpecifiedButNotInRegistry() {
SchemaRegistry registry = SchemaRegistry.builder()
.dialectRegistry(new BasicDialectRegistry(Dialects.getDraft201909())).build();
assertThrows(InvalidSchemaException.class, () -> {
registry.getSchema("{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"type\":\"object\"}");
});
}
}