org.isda.cdm.validation.exists.ChildOnlyExistsValidator Maven / Gradle / Ivy
package org.isda.cdm.validation.exists;
import com.google.common.collect.ImmutableMap;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.validation.ExistenceChecker;
import com.rosetta.model.lib.validation.ValidationResult;
import com.rosetta.model.lib.validation.ValidationResult.ValidationType;
import com.rosetta.model.lib.validation.ValidatorWithArg;
import com.rosetta.model.metafields.ReferenceWithMetaString;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.isda.cdm.AnotherMiddle;
import org.isda.cdm.Child;
import org.isda.cdm.Flat;
import org.isda.cdm.Middle;
import static com.rosetta.model.lib.validation.ValidationResult.failure;
import static com.rosetta.model.lib.validation.ValidationResult.success;
public class ChildOnlyExistsValidator implements ValidatorWithArg> {
/* Casting is required to ensure types are output to ensure recompilation in Rosetta */
@Override
public ValidationResult validate(RosettaPath path, T2 o, Set fields) {
Map fieldExistenceMap = ImmutableMap.builder()
.put("parentFieldOne", ExistenceChecker.isSet((Flat) o.getParentFieldOne()))
.put("parentFieldTwo", ExistenceChecker.isSet((List extends Flat>) o.getParentFieldTwo()))
.put("parentFieldThree", ExistenceChecker.isSet((String) o.getParentFieldThree()))
.put("parentFieldFour", ExistenceChecker.isSet((List) o.getParentFieldFour()))
.put("parentFieldFiveReference", ExistenceChecker.isSet((ReferenceWithMetaString) o.getParentFieldFiveReference()))
.put("parentFieldSix", ExistenceChecker.isSet((Flat) o.getParentFieldSix()))
.put("repeatedField", ExistenceChecker.isSet((List extends Flat>) o.getRepeatedField()))
.put("parentFieldSeven", ExistenceChecker.isSet((Middle) o.getParentFieldSeven()))
.put("parentFieldEight", ExistenceChecker.isSet((AnotherMiddle) o.getParentFieldEight()))
.put("childFieldOne", ExistenceChecker.isSet((Flat) o.getChildFieldOne()))
.put("childFieldTwo", ExistenceChecker.isSet((List extends Flat>) o.getChildFieldTwo()))
.put("childFieldThree", ExistenceChecker.isSet((String) o.getChildFieldThree()))
.put("childFieldFour", ExistenceChecker.isSet((List) o.getChildFieldFour()))
.build();
// Find the fields that are set
Set setFields = fieldExistenceMap.entrySet().stream()
.filter(Map.Entry::getValue)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
if (setFields.equals(fields)) {
return success("Child", ValidationType.ONLY_EXISTS, "Child", path, "");
}
return failure("Child", ValidationType.ONLY_EXISTS, "Child", path, "",
String.format("[%s] should only be set. Set fields: %s", fields, setFields));
}
}