use of com.linkedin.data.schema.validator.DataSchemaAnnotationValidator in project rest.li by linkedin.
the class ComplexResourceKey method validateDataAgainstSchema.
private static void validateDataAgainstSchema(DataMap value, DataSchema schema) {
DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(schema);
ValidationResult validationResult = ValidateDataAgainstSchema.validate(value, schema, new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE), validator);
if (!validationResult.isValid()) {
throw new IllegalArgumentException(String.format("value '%s' is invalid, reason: %s", value, validationResult.getMessages()));
}
}
use of com.linkedin.data.schema.validator.DataSchemaAnnotationValidator in project rest.li by linkedin.
the class TestEmptyRecord method testEmpty.
@Test
public void testEmpty() {
final EmptyRecord record = new EmptyRecord();
final DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(record.schema());
final ValidationResult result = ValidateDataAgainstSchema.validate(record.data(), record.schema(), new ValidationOptions(), validator);
Assert.assertTrue(result.isValid());
}
use of com.linkedin.data.schema.validator.DataSchemaAnnotationValidator 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.validator.DataSchemaAnnotationValidator in project rest.li by linkedin.
the class TestEmptyRecord method testNonEmpty.
@Test
public void testNonEmpty() {
final EmptyRecord record = new EmptyRecord();
record.data().put("non", "empty");
final DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(record.schema());
final ValidationResult result = ValidateDataAgainstSchema.validate(record.data(), record.schema(), new ValidationOptions(), validator);
Assert.assertFalse(result.isValid());
}
Aggregations