All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.networknt.oas.validator.OverlayValidator Maven / Gradle / Ivy

There is a newer version: 2.1.38
Show newest version
/*******************************************************************************
 *  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.fasterxml.jackson.databind.JsonNode;
import com.networknt.oas.jsonoverlay.*;

import java.util.Collections;
import java.util.Set;

import static com.networknt.oas.validator.Messages.m;

public class OverlayValidator extends ValidatorBase {

	@Override
	public void validate(T overlay, ValidationResults results) {
		checkReferences((JsonOverlay) overlay, results);
	}

	public void validate(T overlay, ValidationResults results, Class allowedNodeType) {
		validate(overlay, results, Collections.>singleton(allowedNodeType));
	}

	public void validate(T object, ValidationResults results, Set> allowedNodeTypes) {
		JsonOverlay overlay = (JsonOverlay) object;
		JsonNode json = overlay.toJson();
		boolean isValidJsonType = false;
		for (Class type : allowedNodeTypes) {
			if (type.isAssignableFrom(json.getClass())) {
				isValidJsonType = true;
				break;
			}
		}
		isValidJsonType = isValidJsonType || json.isMissingNode();
		if (!isValidJsonType) {
			results.addError(m.msg("WrongTypeJson|Property bound to incompatible JSON Node type", json.getNodeType(),
					allowedNodeTypes));
		}
	}

	private void checkReferences(JsonOverlay overlay, ValidationResults results) {
		if (overlay instanceof ListOverlay) {
			checkReferences((ListOverlay) overlay, results);
		} else if (overlay instanceof MapOverlay) {
			checkReferences((MapOverlay) overlay, results);
		} else if (overlay instanceof PropertiesOverlay) {
			checkReferences((PropertiesOverlay) overlay, results);
		}
	}

	private void checkReferences(ListOverlay list, ValidationResults results) {
		for (int i = 0; i < list.size(); i++) {
			if (list.isReference(i)) {
				checkReference(list.getReference(i), results, Integer.toString(i));
			}
		}
	}

	private void checkReferences(MapOverlay map, ValidationResults results) {
		for (String key : map.get().keySet()) {
			if (map.isReference(key)) {
				checkReference(map.getReference(key), results, key);
			}
		}
	}

	private void checkReferences(PropertiesOverlay props, ValidationResults results) {
		if (props.isElaborated()) {
			for (String path : props.getRefablePaths()) {
				if (props.isReference(path)) {
					checkReference(props.getReference(path), results, path);
				}
			}
		}
	}

	private void checkReference(Reference ref, ValidationResults results, String crumb) {
		if (ref.isInvalid()) {
			results.addError(
					m.msg("BadRef|JSON reference node was not resolved", ref.getRefString(), ref.getErrorReason()),
					crumb);
		}

	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy