Search in sources :

Example 1 with JsonPath

use of oap.json.schema.JsonPath in project oap by oaplatform.

the class DictionaryJsonValidator method validate.

private Result<List<Dictionary>, List<String>> validate(JsonValidatorProperties properties, Optional<DictionarySchemaAST> schemaOpt, List<Dictionary> dictionaries) {
    if (!schemaOpt.isPresent())
        return Result.success(dictionaries);
    final DictionarySchemaAST schema = schemaOpt.get();
    final Result<List<Dictionary>, List<String>> cd = validate(properties, schema.parent, dictionaries);
    if (!cd.isSuccess())
        return cd;
    final JsonPath jsonPath = new JsonPath(rightTrimItems(schema.path), properties.path);
    List<Object> parentValues = jsonPath.traverse(properties.rootJson);
    if (parentValues.isEmpty()) {
        final String fixedPath = jsonPath.getFixedPath();
        if (!schema.common.required.orElse(BooleanReference.FALSE).apply(properties.rootJson, properties.rootJson, Optional.of(fixedPath), properties.prefixPath))
            // return Result.failure( Lists.of( properties.error( fixedPath, "required property is missing" ) ) );
            // else
            parentValues = cd.successValue.stream().flatMap(d -> d.getValues().stream().map(Dictionary::getId)).collect(toList());
    }
    final ArrayList<Dictionary> cDict = new ArrayList<>();
    for (Object parentValue : parentValues) {
        List<Dictionary> children = cd.successValue.stream().map(d -> d.getValueOpt(parentValue.toString())).filter(Optional::isPresent).map(Optional::get).collect(toList());
        if (children.isEmpty())
            return Result.failure(Lists.of(properties.error("instance does not match any member resolve the enumeration " + printIds(cd.successValue))));
        cDict.addAll(children);
    }
    return Result.success(cDict);
}
Also used : Result(oap.util.Result) Dictionary(oap.dictionary.Dictionary) BooleanReference(oap.json.schema.BooleanReference) SchemaPath.rightTrimItems(oap.json.schema.SchemaPath.rightTrimItems) JsonSchemaParserContext(oap.json.schema.JsonSchemaParserContext) DictionaryNotFoundError(oap.dictionary.DictionaryNotFoundError) JsonSchemaValidator(oap.json.schema.JsonSchemaValidator) ArrayList(java.util.ArrayList) Slf4j(lombok.extern.slf4j.Slf4j) Dictionaries(oap.dictionary.Dictionaries) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SchemaPath(oap.json.schema.SchemaPath) Lists(oap.util.Lists) Optional(java.util.Optional) JsonPath(oap.json.schema.JsonPath) JsonValidatorProperties(oap.json.schema.JsonValidatorProperties) Dictionary(oap.dictionary.Dictionary) Optional(java.util.Optional) ArrayList(java.util.ArrayList) JsonPath(oap.json.schema.JsonPath) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Dictionaries (oap.dictionary.Dictionaries)1 Dictionary (oap.dictionary.Dictionary)1 DictionaryNotFoundError (oap.dictionary.DictionaryNotFoundError)1 BooleanReference (oap.json.schema.BooleanReference)1 JsonPath (oap.json.schema.JsonPath)1 JsonSchemaParserContext (oap.json.schema.JsonSchemaParserContext)1 JsonSchemaValidator (oap.json.schema.JsonSchemaValidator)1 JsonValidatorProperties (oap.json.schema.JsonValidatorProperties)1 SchemaPath (oap.json.schema.SchemaPath)1 SchemaPath.rightTrimItems (oap.json.schema.SchemaPath.rightTrimItems)1 Lists (oap.util.Lists)1 Result (oap.util.Result)1