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 RestLiDataValidator method buildRecordDataSchemaByProjection.
/**
* Build a new {@link RecordDataSchema} schema that contains only the masked fields.
*/
private static RecordDataSchema buildRecordDataSchemaByProjection(RecordDataSchema originalSchema, DataMap maskMap) {
RecordDataSchema newRecordSchema = new RecordDataSchema(new Name(originalSchema.getFullName()), RecordDataSchema.RecordType.RECORD);
List<RecordDataSchema.Field> newFields = new ArrayList<RecordDataSchema.Field>();
for (Map.Entry<String, Object> maskEntry : maskMap.entrySet()) {
RecordDataSchema.Field originalField = originalSchema.getField(maskEntry.getKey());
DataSchema fieldSchemaToUse = reuseOrBuildDataSchema(originalField.getType(), maskEntry.getValue());
RecordDataSchema.Field newField = buildRecordField(originalField, fieldSchemaToUse, newRecordSchema);
newFields.add(newField);
}
// Fields from 'include' are no difference from other fields from original schema,
// therefore, we are not calling newRecordSchema.setInclude() here.
// No errors are expected here, as the new schema is merely subset of the original
newRecordSchema.setFields(newFields, new StringBuilder());
if (originalSchema.getAliases() != null) {
newRecordSchema.setAliases(originalSchema.getAliases());
}
if (originalSchema.getDoc() != null) {
newRecordSchema.setDoc(originalSchema.getDoc());
}
if (originalSchema.getProperties() != null) {
newRecordSchema.setProperties(originalSchema.getProperties());
}
return newRecordSchema;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class RestLiDataValidator method validateOutputEntity.
private ValidationResult validateOutputEntity(RecordTemplate entity, MaskTree projectionMask) {
try {
// Value class from resource model is the only source of truth for record schema.
// Schema from the record template itself should not be used.
DataSchema originalSchema = DataTemplateUtil.getSchema(_valueClass);
// When a projection is defined, we only need to validate the projected fields.
DataSchema validatingSchema = (projectionMask != null) ? buildSchemaByProjection(originalSchema, projectionMask.getDataMap()) : originalSchema;
DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(validatingSchema);
return ValidateDataAgainstSchema.validate(entity.data(), validatingSchema, new ValidationOptions(), validator);
} catch (TemplateRuntimeException e) {
return validationResultWithErrorMessage(TEMPLATE_RUNTIME_ERROR);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildTyperefDataSchemaByProjection.
/**
* Build a new {@link TyperefDataSchema} schema that contains only the masked fields.
*/
private static TyperefDataSchema buildTyperefDataSchemaByProjection(TyperefDataSchema originalSchema, DataMap maskMap) {
TyperefDataSchema newSchema = new TyperefDataSchema(new Name(originalSchema.getFullName()));
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
if (originalSchema.getDoc() != null) {
newSchema.setDoc(originalSchema.getDoc());
}
if (originalSchema.getAliases() != null) {
newSchema.setAliases(originalSchema.getAliases());
}
DataSchema newRefSchema = buildSchemaByProjection(originalSchema.getRef(), maskMap);
newSchema.setReferencedType(newRefSchema);
return newSchema;
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class RestLiDataValidator method buildUnionDataSchemaByProjection.
/**
* Build a new {@link UnionDataSchema} schema that contains only the masked fields.
*/
private static UnionDataSchema buildUnionDataSchemaByProjection(UnionDataSchema originalSchema, DataMap maskMap) {
List<DataSchema> newUnionTypeSchemas = new ArrayList<>();
for (Map.Entry<String, Object> maskEntry : maskMap.entrySet()) {
DataSchema originalTypeSchema = originalSchema.getType(maskEntry.getKey());
DataSchema typeSchemaToUse = reuseOrBuildDataSchema(originalTypeSchema, maskEntry.getValue());
newUnionTypeSchemas.add(typeSchemaToUse);
}
UnionDataSchema newSchema = new UnionDataSchema();
// No errors are expected here, as the new schema is merely subset of the orignal
newSchema.setTypes(newUnionTypeSchemas, new StringBuilder());
if (originalSchema.getProperties() != null) {
newSchema.setProperties(originalSchema.getProperties());
}
return newSchema;
}
Aggregations