-
Notifications
You must be signed in to change notification settings - Fork 341
fixes #1174 TextNodes as schema seem to validate any value #1250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03d4496
fixes #1174 TextNodes as schema seem to validate any value
stevehu 116e818
Address loaded schema validation for issue 1174
stevehu b510def
Address schema node validation review comments
stevehu 3e9ec3e
Preserve mapped schema override path
stevehu b1e9587
Validate referenced document fragment schemas
stevehu 4587ac5
Remove duplicate schema type validation
stevehu ed04ee9
Validate loaded schemas for anchor refs
stevehu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Copyright (c) 2026 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; | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import tools.jackson.databind.node.JsonNodeFactory; | ||
|
|
||
| class Issue1174Test { | ||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsRootSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, | ||
| () -> registry.getSchema(JsonNodeFactory.instance.textNode("false"))); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeContainingJsonShouldNotBeAcceptedAsRootSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, | ||
| () -> registry.getSchema(JsonNodeFactory.instance.textNode("{\"type\":\"string\"}"))); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsLoadedRootSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7, | ||
| builder -> builder.schemas(Collections.singletonMap("https://www.example.org/text-schema", "\"false\""))); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, | ||
| () -> registry.getSchema(SchemaLocation.of("https://www.example.org/text-schema"))); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsReferencedRootSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7, | ||
| builder -> builder.schemas(Collections.singletonMap("https://www.example.org/text-schema", "\"false\""))); | ||
| Schema schema = registry.getSchema("{\"$ref\":\"https://www.example.org/text-schema\"}"); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, () -> schema.validate("42", InputFormat.JSON)); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsReferencedDocumentFragmentSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7, | ||
| builder -> builder.schemas(Collections.singletonMap("https://www.example.org/text-schema", "\"false\""))); | ||
| Schema schema = registry.getSchema("{\"$ref\":\"https://www.example.org/text-schema#\"}"); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, () -> schema.validate("42", InputFormat.JSON)); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsReferencedAnchorSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7, | ||
| builder -> builder.schemas(Collections.singletonMap("https://www.example.org/text-schema", "\"false\""))); | ||
| Schema schema = registry.getSchema("{\"$ref\":\"https://www.example.org/text-schema#myAnchor\"}"); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, () -> schema.validate("42", InputFormat.JSON)); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void textNodeShouldNotBeAcceptedAsSubSchema() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, | ||
| () -> registry.getSchema("{\"type\":\"object\",\"properties\":{\"a\":\"false\"}}")); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object or boolean")); | ||
| assertTrue(exception.getMessage().contains("STRING")); | ||
| } | ||
|
|
||
| @Test | ||
| void booleanSchemaShouldBeAcceptedForDraft7() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7); | ||
| Schema schema = registry.getSchema("false"); | ||
|
|
||
| List<Error> errors = schema.validate("42", InputFormat.JSON); | ||
|
|
||
| assertEquals(1, errors.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void loadedBooleanSchemaShouldBeAcceptedForDraft7() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_7, | ||
| builder -> builder.schemas(Collections.singletonMap("https://www.example.org/false-schema", "false"))); | ||
| Schema schema = registry.getSchema(SchemaLocation.of("https://www.example.org/false-schema")); | ||
|
|
||
| List<Error> errors = schema.validate("42", InputFormat.JSON); | ||
|
|
||
| assertEquals(1, errors.size()); | ||
| } | ||
|
|
||
| @Test | ||
| void booleanSchemaShouldNotBeAcceptedForDraft4() { | ||
| SchemaRegistry registry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_4); | ||
|
|
||
| SchemaException exception = assertThrows(SchemaException.class, () -> registry.getSchema("false")); | ||
|
|
||
| assertTrue(exception.getMessage().contains("must be object")); | ||
| assertTrue(exception.getMessage().contains("BOOLEAN")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.