use of com.linkedin.data.schema.validation.ValidationOptions in project rest.li by linkedin.
the class TestSchemaSampleDataGenerator method testRecordSchema.
@Test
public void testRecordSchema() {
final RecordDataSchema schema = (RecordDataSchema) DataTemplateUtil.getSchema(Certification.class);
final DataMap value = SchemaSampleDataGenerator.buildRecordData(schema, _spec);
final ValidationResult result = ValidateDataAgainstSchema.validate(value, schema, new ValidationOptions());
Assert.assertTrue(result.isValid(), Arrays.toString(result.getMessages().toArray()));
}
use of com.linkedin.data.schema.validation.ValidationOptions 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.validation.ValidationOptions in project rest.li by linkedin.
the class QueryParamsDataMap method fixUpComplexKeySingletonArray.
/**
* Because of backwards compatibility concerns, array fields of the key component of a
* {@link ComplexResourceKey}s in a get request will be represented in the request url in the old
* style. That is, if an array field has the name "a", and contains [1,2] the part of the url
* representing the serialized array will look like "a=1&a=2". However, if the array is a
* singleton it will just be represented by "a=1". Therefore it is not possible to distinguish
* between a single value itself and an array containing a single value.
*
* The purpose of this function is to fix up the singleton array problem by checking to see whether the given
* ComplexKey's key part has an array component, and, if so and the data for that field is NOT a dataList,
* placing the data into a dataList.
*
* @param complexResourceKey The complex key to be fixed.
*/
public static ComplexResourceKey<?, ?> fixUpComplexKeySingletonArray(ComplexResourceKey<?, ?> complexResourceKey) {
RecordTemplate key = complexResourceKey.getKey();
DataMap dataMap = key.data();
List<RecordDataSchema.Field> fields = key.schema() == null ? Collections.<RecordDataSchema.Field>emptyList() : key.schema().getFields();
for (RecordDataSchema.Field f : fields) {
DataSchema.Type type = f.getType().getType();
String fieldName = f.getName();
if (type == DataSchema.Type.ARRAY && dataMap.containsKey(fieldName)) {
Object arrayFieldValue = dataMap.get(fieldName);
if (!(arrayFieldValue instanceof DataList)) {
DataList list = new DataList();
list.add(arrayFieldValue);
ValidateDataAgainstSchema.validate(list, f.getType(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
dataMap.put(fieldName, list);
}
}
}
RecordTemplate wrappedKey = DataTemplateUtil.wrap(dataMap, key.getClass());
@SuppressWarnings("unchecked") ComplexResourceKey<?, ?> newKey = new ComplexResourceKey<RecordTemplate, RecordTemplate>(wrappedKey, complexResourceKey.getParams());
return newKey;
}
use of com.linkedin.data.schema.validation.ValidationOptions in project rest.li by linkedin.
the class ComplexResourceKey method validateDataMap.
private static RecordTemplate validateDataMap(DataMap dataMap, TypeSpec<? extends RecordTemplate> spec) {
RecordTemplate recordTemplate = wrapWithSchema(dataMap, spec);
// Validate against the class schema with FixupMode.STRING_TO_PRIMITIVE to parse the
// strings into the
// corresponding primitive types.
ValidateDataAgainstSchema.validate(recordTemplate.data(), recordTemplate.schema(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
return recordTemplate;
}
use of com.linkedin.data.schema.validation.ValidationOptions 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