com.networknt.oas.validator.ValidationResults Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-parser Show documentation
Show all versions of openapi-parser Show documentation
A light-weight, fast OpenAPI 3.0 parser and validator
/*******************************************************************************
* Copyright (c) 2017 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/
package com.networknt.oas.validator;
import com.networknt.oas.jsonoverlay.JsonOverlay;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static com.networknt.oas.validator.ValidationResults.Severity.*;
public class ValidationResults {
public enum Severity {
NONE, INFO, WARNING, ERROR;
public static final Severity MAX_SEVERITY = ERROR;
public boolean lt(Severity other) {
return this.compareTo(other) < 0;
}
public boolean le(Severity other) {
return this.compareTo(other) <= 0;
}
public boolean gt(Severity other) {
return this.compareTo(other) > 0;
}
public boolean ge(Severity other) {
return this.compareTo(other) >= 0;
}
};
List items = new ArrayList<>();
List crumbs = Collections.emptyList();
public void addInfo(String msg) {
items.add(new ValidationItem(INFO, msg, crumbs));
}
public void addInfo(String msg, String crumb) {
items.add(new ValidationItem(INFO, msg, crumbs, crumb));
}
public void addWarning(String msg) {
items.add(new ValidationItem(WARNING, msg, crumbs));
}
public void addWarning(String msg, String crumb) {
items.add(new ValidationItem(WARNING, msg, crumbs, crumb));
}
public void addError(String msg) {
items.add(new ValidationItem(ERROR, msg, crumbs));
}
public void addError(String msg, String crumb) {
items.add(new ValidationItem(ERROR, msg, crumbs, crumb));
}
public void add(ValidationResults results) {
items.addAll(results.getItems());
}
public Collection getItems() {
return items;
}
public Severity getSeverity() {
Severity severity = NONE;
for (ValidationItem item : items) {
if (item.getSeverity().gt(severity)) {
severity = item.getSeverity();
if (severity == MAX_SEVERITY) {
break;
}
}
}
return severity;
}
public > void validateWithCrumb(String crumb, Validator validator, T object) {
List priorCrumbs = crumbs;
try {
if (crumb != null) {
crumbs = appendCrumb(crumb, priorCrumbs);
}
validator.validate(object, this);
} finally {
crumbs = priorCrumbs;
}
}
public void withCrumb(String crumb, Runnable code) {
List priorCrumbs = crumbs;
try {
crumbs = appendCrumb(crumb, priorCrumbs);
code.run();
} finally {
crumbs = priorCrumbs;
}
}
private static List appendCrumb(String crumb, List existingCrumbs) {
if (crumb != null) {
List newCrumbs = new ArrayList<>(existingCrumbs);
newCrumbs.add(crumb);
return newCrumbs;
} else {
return existingCrumbs;
}
}
public static class ValidationItem {
private Severity severity;
private String msg;
private List crumbs;
public ValidationItem(Severity severity, String msg, List crumbs) {
super();
this.severity = severity;
this.msg = msg;
this.crumbs = crumbs;
}
public ValidationItem(Severity severity, String msg, List crumbs, String crumb) {
this(severity, msg, appendCrumb(crumb, crumbs));
}
public Severity getSeverity() {
return severity;
}
public String getMsg() {
return msg;
}
public List getCrumbs() {
return crumbs;
}
@Override
public String toString() {
String label = crumbs != null & !crumbs.isEmpty() ? StringUtils.join(crumbs, '.') + ": " : "";
return label + msg;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy