Search in sources :

Example 1 with DataSchemaAnnotationValidator

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()));
    }
}
Also used : DataSchemaAnnotationValidator(com.linkedin.data.schema.validator.DataSchemaAnnotationValidator) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions)

Example 2 with DataSchemaAnnotationValidator

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());
}
Also used : DataSchemaAnnotationValidator(com.linkedin.data.schema.validator.DataSchemaAnnotationValidator) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) Test(org.testng.annotations.Test)

Example 3 with DataSchemaAnnotationValidator

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);
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) DataSchemaAnnotationValidator(com.linkedin.data.schema.validator.DataSchemaAnnotationValidator) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions)

Example 4 with DataSchemaAnnotationValidator

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());
}
Also used : DataSchemaAnnotationValidator(com.linkedin.data.schema.validator.DataSchemaAnnotationValidator) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) Test(org.testng.annotations.Test)

Aggregations

ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)4 DataSchemaAnnotationValidator (com.linkedin.data.schema.validator.DataSchemaAnnotationValidator)4 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)3 Test (org.testng.annotations.Test)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 MapDataSchema (com.linkedin.data.schema.MapDataSchema)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)1