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

com.jetdrone.vertx.yoke.json.AnyValidator Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.jetdrone.vertx.yoke.json;

import java.util.List;
import java.util.Map;

public final class AnyValidator {

    public static boolean isValid(Object instance, JsonSchemaResolver.Schema schema) {
        // validate required
        if (instance == null) {
            if (Boolean.TRUE.equals(schema.get("required"))) {
                return false;
            }
        }

        // apply default value
        if (instance == null) {
            instance = schema.get("default");
        }

        if (instance != null) {
            // validate enum
            List _enum = schema.get("enum");
            if (_enum != null && !_enum.contains(instance)) {
                return false;
            }

            // TODO: type

            // validate allOf
            List allOf = schema.get("allOf");
            if (allOf != null) {
                for (int i = 0; i < allOf.size(); i++) {
                    Object item = allOf.get(i);

                    if (item instanceof Map) {
                        // convert to schema
                        item = JsonSchemaResolver.resolveSchema((Map) item);
                        allOf.set(i, item);
                    }

                    if (!JsonSchema.conformsSchema(instance, (JsonSchemaResolver.Schema) item)) {
                        return false;
                    }
                }
            }

            // validate anyOf
            List anyOf = schema.get("anyOf");
            if (anyOf != null) {
                boolean match = false;
                for (int i = 0; i < anyOf.size(); i++) {
                    Object item = anyOf.get(i);

                    if (item instanceof Map) {
                        // convert to schema
                        item = JsonSchemaResolver.resolveSchema((Map) item);
                        anyOf.set(i, item);
                    }

                    if (JsonSchema.conformsSchema(instance, (JsonSchemaResolver.Schema) item)) {
                        match = true;
                        break;
                    }
                }
                if (!match) {
                    return false;
                }
            }

            // validate oneOf
            List oneOf = schema.get("oneOf");
            if (oneOf != null) {
                int matches = 0;
                for (int i = 0; i < oneOf.size(); i++) {
                    Object item = oneOf.get(i);

                    if (item instanceof Map) {
                        // convert to schema
                        item = JsonSchemaResolver.resolveSchema((Map) item, schema.getParent());
                        oneOf.set(i, item);
                    }

                    if (JsonSchema.conformsSchema(instance, (JsonSchemaResolver.Schema) item)) {
                        matches++;
                    }
                }
                if (matches == 0) {
                    return false;
                }
            }

            // validate not
            Object not = schema.get("not");
            if (not != null) {
                if (not instanceof Map) {
                    // convert to schema
                    not = JsonSchemaResolver.resolveSchema((Map) not, schema.getParent());
                    schema.put("not", not);
                }

                if (JsonSchema.conformsSchema(instance, (JsonSchemaResolver.Schema) not)) {
                    return false;
                }
            }

            // TODO: definitions
        }

        return true;
    }
}