use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestConversions method testConvertDataMapToDataSchema.
@Test
public void testConvertDataMapToDataSchema() throws IOException {
for (String good : goodInputs) {
NamedDataSchema dataSchema = (NamedDataSchema) TestUtil.dataSchemaFromString(good);
DataMap mapFromString = TestUtil.dataMapFromString(good);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertEquals(schemaFromMap, dataSchema);
}
for (String bad : badInputs) {
DataMap mapFromString = TestUtil.dataMapFromString(bad);
PegasusSchemaParser parser = new SchemaParser();
DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
assertNull(schemaFromMap);
assertTrue(parser.hasError());
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestValidation method testJsonValidation.
@Test
public void testJsonValidation() throws IOException {
Object[][] input = { { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"intField\", \"type\" : \"int\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + ((long) Integer.MAX_VALUE + 1) + " }", "ERROR :: /intField :: 2147483648 is not backed by a Integer" } }, { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"longField\", \"type\" : \"long\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MIN_VALUE + " }" } } };
for (Object[] row : input) {
String schemaText = (String) row[0];
for (int i = 1; i < row.length; i++) {
Object[] test = (Object[]) row[i];
ValidationOptions options = (ValidationOptions) test[0];
String dataText = (String) test[1];
String expectedResult = test.length > 2 ? (String) test[2] : null;
DataSchema schema = dataSchemaFromString(schemaText);
DataMap dataMap = dataMapFromString(dataText);
ValidationResult result = ValidateDataAgainstSchema.validate(dataMap, schema, options);
if (expectedResult == null) {
Assert.assertTrue(result.isValid());
Assert.assertEquals(result.getMessages().size(), 0);
} else {
Assert.assertTrue(result.toString().contains(expectedResult));
}
}
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestValidation method testUnrecognizedFieldTrimming.
@Test
public void testUnrecognizedFieldTrimming() throws IOException {
ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
String schemaText = "{\n" + " \"name\" : \"Foo\",\n" + " \"type\" : \"record\",\n" + " \"fields\" : [\n" + " { \"name\" : \"primitive\", \"type\" : \"int\", \"optional\" : true },\n" + " { \"name\" : \"arrayField\", \"type\" : { \"type\" : \"array\", \"items\" : \"Foo\" }, \"optional\" : true },\n" + " { \"name\" : \"mapField\", \"type\" : { \"type\" : \"map\", \"values\" : \"Foo\" }, \"optional\" : true },\n" + " { \"name\" : \"unionField\", \"type\" : [ \"int\", \"string\", \"Foo\" ], \"optional\" : true },\n" + " { \"name\" : \"recordField\", \"type\" : \"Foo\", \"optional\" : true }\n" + " ]\n" + "}\n";
DataSchema schema = dataSchemaFromString(schemaText);
String dataString = "{\n" + " \"primitive\" : 1,\n" + " \"unrecognizedPrimitive\": -1,\n" + " \"arrayField\" : [ { \"primitive\": 2, \"unrecognizedInArray\": -2 } ],\n" + " \"mapField\" : { \"key\": { \"primitive\": 3, \"unrecognizedInMap\": -3 } },\n" + " \"unionField\" : { \"Foo\": { \"primitive\": 4, \"unrecognizedInMap\": -4 } },\n" + " \"recordField\" : {\n" + " \"primitive\" : 5,\n" + " \"unrecognizedPrimitive\": -5\n" + " },\n" + " \"unrecognizedMap\": { \"key\": -100},\n" + " \"unrecognizedArray\": [ -101 ]\n" + "}";
String trimmedDataString = "{\n" + " \"primitive\" : 1,\n" + " \"arrayField\" : [ { \"primitive\": 2 } ],\n" + " \"mapField\" : { \"key\": { \"primitive\": 3 } },\n" + " \"unionField\" : { \"Foo\": { \"primitive\": 4 } },\n" + " \"recordField\" : {\n" + " \"primitive\" : 5\n" + " }\n" + "}";
// mutable
DataMap toValidate = dataMapFromString(dataString);
ValidationResult result = validate(toValidate, schema, options);
Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
Assert.assertSame(toValidate, result.getFixed());
// read-only
DataMap readOnlyToValidate = dataMapFromString(dataString);
readOnlyToValidate.makeReadOnly();
ValidationResult readOnlyResult = validate(readOnlyToValidate, schema, options);
Assert.assertTrue(readOnlyResult.hasFixupReadOnlyError());
String message = readOnlyResult.getMessages().toString();
Assert.assertEquals(readOnlyResult.getMessages().size(), 7);
String[] expectedStrings = new String[] { "/unrecognizedMap", "/unrecognizedArray", "/recordField/unrecognizedPrimitive", "/unionField/Foo/unrecognizedInMap", "/unrecognizedPrimitive", "/arrayField/0/unrecognizedInArray", "/mapField/key/unrecognizedInMap" };
for (String expected : expectedStrings) {
Assert.assertTrue(message.contains(expected), message + " does not contain " + expected);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestValidation method testTrimUnrecognizedFieldsWithAvroUnionDefault.
@Test
public void testTrimUnrecognizedFieldsWithAvroUnionDefault() throws IOException {
ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
options.setAvroUnionMode(true);
String schemaText = "{\n" + " \"name\" : \"Foo\",\n" + " \"type\" : \"record\",\n" + " \"fields\" : [\n" + " { \"name\" : \"primitive\", \"type\" : \"int\", \"optional\" : true },\n" + " { \"name\" : \"unionField\", \"type\" : [ \"Foo\", \"int\", \"string\" ], \"optional\" : true }\n" + " ]\n" + "}\n";
DataSchema schema = dataSchemaFromString(schemaText);
String dataString = "{\n" + " \"unionField\" : { \"primitive\": 4, \"unrecognizedInMap\": -4 }\n" + "}";
String trimmedDataString = "{\n" + " \"unionField\" : { \"primitive\": 4 }\n" + "}";
DataMap toValidate = dataMapFromString(dataString);
ValidationResult result = validate(toValidate, schema, options);
Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
Assert.assertSame(toValidate, result.getFixed());
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class DataSchemaAnnotationValidator method validateSchema.
private final void validateSchema(ValidatorContext context, DataSchema schema) {
if (schema.getType() == DataSchema.Type.TYPEREF) {
DataSchema refSchema = ((TyperefDataSchema) schema).getRef();
validateSchema(context, refSchema);
getAndInvokeValidatorList(context, schema);
} else {
getAndInvokeValidatorList(context, schema);
}
}
Aggregations