Skip to content
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

Add test for type integer #954

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
47 changes: 47 additions & 0 deletions src/test/java/com/networknt/schema/TypeValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,51 @@ void testTypeLoose() {
assertEquals(1, messages.size());

}

/**
* Issue 864.
*/
@Test
void integer() {
String schemaData = "{\r\n"
+ " \"type\": \"integer\"\r\n"
+ "}";
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData);
Set<ValidationMessage> messages = schema.validate("1", InputFormat.JSON);
assertEquals(0, messages.size());
messages = schema.validate("2.0", InputFormat.JSON);
assertEquals(0, messages.size());
messages = schema.validate("2.000001", InputFormat.JSON);
assertEquals(1, messages.size());
}

/**
* Issue 864.
* <p>
* In draft-04, "integer" is listed as a primitive type and defined as "a JSON
* number without a fraction or exponent part"; in draft-06, "integer" is not
* considered a primitive type and is only defined in the section for keyword
* "type" as "any number with a zero fractional part"; 1.0 is thus not a valid
* "integer" type in draft-04 and earlier, but is a valid "integer" type in
* draft-06 and later; note that both drafts say that integers SHOULD be encoded
* in JSON without fractional parts
*
* @see <a href=
* "https://json-schema.org/draft-06/json-schema-release-notes">Draft-06
* Release Notes</a>
*/
@Test
void integerDraft4() {
String schemaData = "{\r\n"
+ " \"type\": \"integer\"\r\n"
+ "}";
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V4).getSchema(schemaData);
Set<ValidationMessage> messages = schema.validate("1", InputFormat.JSON);
assertEquals(0, messages.size());
// The logic in JsonNodeUtil specifically excludes V4 from this handling
messages = schema.validate("2.0", InputFormat.JSON);
assertEquals(1, messages.size());
messages = schema.validate("2.000001", InputFormat.JSON);
assertEquals(1, messages.size());
}
}
Loading