-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for type loose for array and update doc for behavior (#946)
- Loading branch information
1 parent
c7e7ab4
commit 91385c2
Showing
3 changed files
with
148 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 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,52 @@ | ||
/* | ||
* Copyright (c) 2016 Network New Technologies Inc. | ||
* | ||
* 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; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.networknt.schema.utils.StringChecker.isNumeric; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class StringCheckerTest { | ||
|
||
private static final String[] validNumericValues = { | ||
"1", "-1", "1.1", "-1.1", "0E+1", "0E-1", "0E1", "-0E+1", "-0E-1", "-0E1", "0.1E+1", "0.1E-1", "0.1E1", | ||
"-0.1E+1", "-0.1E-1", "-0.1E1", "10.1", "-10.1", "10E+1", "10E-1", "10E1", "-10E+1", "-10E-1", "-10E1", | ||
"10.1E+1", "10.1E-1", "10.1E1", "-10.1E+1", "-10.1E-1", "-10.1E1", "1E+0", "1E-0", "1E0", | ||
"1E00000000000000000000" | ||
}; | ||
private static final String[] invalidNumericValues = { | ||
"01.1", "1.", ".1", "0.1.1", "E1", "E+1", "E-1", ".E1", ".E+1", ".E-1", ".1E1", ".1E+1", ".1E-1", "1E-", | ||
"1E+", "1E", "+", "-", "1a", "0.1a", "0E1a", "0E-1a", "1.0a", "1.0aE1" | ||
//, "+0", "+1" // for backward compatibility, in violation of JSON spec | ||
}; | ||
|
||
@Test | ||
void testNumericValues() { | ||
for (String validValue : validNumericValues) { | ||
assertTrue(isNumeric(validValue), validValue); | ||
} | ||
} | ||
|
||
@Test | ||
void testNonNumericValues() { | ||
for (String invalidValue : invalidNumericValues) { | ||
assertFalse(isNumeric(invalidValue), invalidValue); | ||
} | ||
} | ||
} |
147 changes: 95 additions & 52 deletions
147
src/test/java/com/networknt/schema/TypeValidatorTest.java
100755 → 100644
This file contains 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 |
---|---|---|
@@ -1,52 +1,95 @@ | ||
/* | ||
* Copyright (c) 2016 Network New Technologies Inc. | ||
* | ||
* 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; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.networknt.schema.utils.StringChecker.isNumeric; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class TypeValidatorTest { | ||
|
||
private static final String[] validNumericValues = { | ||
"1", "-1", "1.1", "-1.1", "0E+1", "0E-1", "0E1", "-0E+1", "-0E-1", "-0E1", "0.1E+1", "0.1E-1", "0.1E1", | ||
"-0.1E+1", "-0.1E-1", "-0.1E1", "10.1", "-10.1", "10E+1", "10E-1", "10E1", "-10E+1", "-10E-1", "-10E1", | ||
"10.1E+1", "10.1E-1", "10.1E1", "-10.1E+1", "-10.1E-1", "-10.1E1", "1E+0", "1E-0", "1E0", | ||
"1E00000000000000000000" | ||
}; | ||
private static final String[] invalidNumericValues = { | ||
"01.1", "1.", ".1", "0.1.1", "E1", "E+1", "E-1", ".E1", ".E+1", ".E-1", ".1E1", ".1E+1", ".1E-1", "1E-", | ||
"1E+", "1E", "+", "-", "1a", "0.1a", "0E1a", "0E-1a", "1.0a", "1.0aE1" | ||
//, "+0", "+1" // for backward compatibility, in violation of JSON spec | ||
}; | ||
|
||
@Test | ||
public void testNumeicValues() { | ||
for (String validValue : validNumericValues) { | ||
assertTrue(isNumeric(validValue), validValue); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNonNumeicValues() { | ||
for (String invalidValue : invalidNumericValues) { | ||
assertFalse(isNumeric(invalidValue), invalidValue); | ||
} | ||
} | ||
} | ||
/* | ||
* Copyright (c) 2024 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; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.networknt.schema.SpecVersion.VersionFlag; | ||
|
||
/** | ||
* Test TypeValidator validator. | ||
*/ | ||
public class TypeValidatorTest { | ||
String schemaData = "{\r\n" // Issue 415 | ||
+ " \"$schema\": \"http://json-schema.org/draft-07/schema\",\r\n" | ||
+ " \"$id\": \"http://example.com/example.json\",\r\n" | ||
+ " \"type\": \"object\",\r\n" | ||
+ " \"properties\": {\r\n" | ||
+ " \"array_of_integers\": {\r\n" | ||
+ " \"$id\": \"#/properties/array_of_integers\",\r\n" | ||
+ " \"type\": \"array\",\r\n" | ||
+ " \"items\": {\r\n" | ||
+ " \"type\": \"integer\"\r\n" | ||
+ " }\r\n" | ||
+ " },\r\n" | ||
+ " \"array_of_objects\": {\r\n" | ||
+ " \"$id\": \"#/properties/array_of_objects\",\r\n" | ||
+ " \"type\": \"array\",\r\n" | ||
+ " \"items\": {\r\n" | ||
+ " \"type\": \"object\"\r\n" | ||
+ " }\r\n" | ||
+ " }\r\n" | ||
+ " }\r\n" | ||
+ "}"; | ||
|
||
@Test | ||
void testTypeLoose() { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); | ||
JsonSchema schema = factory.getSchema(schemaData); | ||
|
||
String inputData = "{\r\n" | ||
+ " \"array_of_integers\": 1,\r\n" | ||
+ " \"array_of_objects\": {}\r\n" | ||
+ "}"; | ||
String validTypeLooseInputData = "{\r\n" | ||
+ " \"array_of_integers\": [\"1\"],\r\n" | ||
+ " \"array_of_objects\": [{}]\r\n" | ||
+ "}"; | ||
String invalidTypeLooseData = "{\r\n" | ||
+ " \"array_of_integers\": \"a\",\r\n" | ||
+ " \"array_of_objects\": {}\r\n" | ||
+ "}"; | ||
// Without type loose this has 2 type errors | ||
Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); | ||
assertEquals(2, messages.size()); | ||
assertEquals(2, messages.stream().filter(m -> "type".equals(m.getType())).count()); | ||
|
||
// 1 type error in array_of_integers | ||
messages = schema.validate(validTypeLooseInputData, InputFormat.JSON); | ||
assertEquals(1, messages.size()); | ||
assertEquals(1, messages.stream().filter(m -> "type".equals(m.getType())).count()); | ||
|
||
// With type loose this has 0 type errors as any item can also be interpreted as an array of 1 item | ||
SchemaValidatorsConfig config = new SchemaValidatorsConfig(); | ||
config.setTypeLoose(true); | ||
JsonSchema typeLoose = factory.getSchema(schemaData, config); | ||
messages = typeLoose.validate(inputData, InputFormat.JSON); | ||
assertEquals(0, messages.size()); | ||
|
||
// No errors | ||
messages = typeLoose.validate(validTypeLooseInputData, InputFormat.JSON); | ||
assertEquals(0, messages.size()); | ||
|
||
// Error because a string cannot be interpreted as an array of integer | ||
messages = typeLoose.validate(invalidTypeLooseData, InputFormat.JSON); | ||
assertEquals(1, messages.size()); | ||
|
||
} | ||
} |