Search in sources :

Example 21 with ValidationOptions

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()));
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) Certification(com.linkedin.pegasus.generator.test.Certification) DataMap(com.linkedin.data.DataMap) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Example 22 with ValidationOptions

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);
    }
}
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 23 with ValidationOptions

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;
}
Also used : ByteString(com.linkedin.data.ByteString) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 24 with ValidationOptions

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

Example 25 with ValidationOptions

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());
}
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)29 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)16 DataMap (com.linkedin.data.DataMap)13 Test (org.testng.annotations.Test)11 DataSchema (com.linkedin.data.schema.DataSchema)10 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)7 DataList (com.linkedin.data.DataList)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)4 ByteString (com.linkedin.data.ByteString)3 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)3 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)3 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)3 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)3 SchemaParser (com.linkedin.data.schema.SchemaParser)3 DataSchemaAnnotationValidator (com.linkedin.data.schema.validator.DataSchemaAnnotationValidator)3 DataElement (com.linkedin.data.element.DataElement)2 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)2 Message (com.linkedin.data.message.Message)2 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)2