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

com.dimajix.shaded.everit.schema.loader.SpecificationVersion Maven / Gradle / Ivy

There is a newer version: 1.2.0-synapse3.3-spark3.3-hadoop3.3
Show newest version
package com.dimajix.shaded.everit.schema.loader;

import com.dimajix.shaded.everit.schema.FormatValidator;
import com.dimajix.shaded.everit.schema.internal.DateFormatValidator;
import com.dimajix.shaded.everit.schema.internal.DateTimeFormatValidator;
import com.dimajix.shaded.everit.schema.internal.EmailFormatValidator;
import com.dimajix.shaded.everit.schema.internal.HostnameFormatValidator;
import com.dimajix.shaded.everit.schema.internal.IPV4Validator;
import com.dimajix.shaded.everit.schema.internal.IPV6Validator;
import com.dimajix.shaded.everit.schema.internal.JsonPointerFormatValidator;
import com.dimajix.shaded.everit.schema.internal.RegexFormatValidator;
import com.dimajix.shaded.everit.schema.internal.RelativeJsonPointerFormatValidator;
import com.dimajix.shaded.everit.schema.internal.TimeFormatValidator;
import com.dimajix.shaded.everit.schema.internal.URIFormatValidator;
import com.dimajix.shaded.everit.schema.internal.URIReferenceFormatValidator;
import com.dimajix.shaded.everit.schema.internal.URITemplateFormatValidator;
import com.dimajix.shaded.everit.schema.internal.URIV4FormatValidator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

/**
 * @author erosb
 */
public enum SpecificationVersion {

    DRAFT_4 {
        @Override List arrayKeywords() {
            return V4_ARRAY_KEYWORDS;
        }

        @Override List objectKeywords() {
            return V4_OBJECT_KEYWORDS;
        }

        @Override public String idKeyword() {
            return "id";
        }

        @Override List metaSchemaUrls() {
            return Arrays.asList(
                "http://json-schema.org/draft-04/schema",
                "https://json-schema.org/draft-04/schema"
            );
        }

        @Override Map defaultFormatValidators() {
            return V4_VALIDATORS;
        }

    }, DRAFT_6 {
        @Override List arrayKeywords() {
            return V6_ARRAY_KEYWORDS;
        }

        @Override List objectKeywords() {
            return V6_OBJECT_KEYWORDS;
        }

        @Override public String idKeyword() {
            return "$id";
        }

        @Override List metaSchemaUrls() {
            return Arrays.asList(
                "http://json-schema.org/draft-06/schema",
                "https://json-schema.org/draft-06/schema"
            );
        }

        @Override Map defaultFormatValidators() {
            return V6_VALIDATORS;
        }

    }, DRAFT_7 {
        @Override List arrayKeywords() {
            return V6_ARRAY_KEYWORDS;
        }

        @Override List objectKeywords() {
            return V6_OBJECT_KEYWORDS;
        }

        @Override public String idKeyword() {
            return DRAFT_6.idKeyword();
        }

        @Override List metaSchemaUrls() {
            return Arrays.asList(
                "http://json-schema.org/draft-07/schema",
                "https://json-schema.org/draft-07/schema"
            );
        }

        @Override Map defaultFormatValidators() {
            return V7_VALIDATORS;
        }
    };

    static SpecificationVersion getByMetaSchemaUrl(String metaSchemaUrl) {
        return lookupByMetaSchemaUrl(metaSchemaUrl)
                .orElseThrow(() -> new IllegalArgumentException(
                        format("could not determine schema version: no meta-schema is known with URL [%s]", metaSchemaUrl)
                ));
    }

    public static Optional lookupByMetaSchemaUrl(String metaSchemaUrl) {
        return Arrays.stream(values())
                .filter(v -> v.metaSchemaUrls().stream().anyMatch(metaSchemaUrl::startsWith))
                .findFirst();
    }

    private static final List V6_OBJECT_KEYWORDS = keywords("properties", "required",
            "minProperties",
            "maxProperties",
            "dependencies",
            "patternProperties",
            "additionalProperties",
            "propertyNames");

    private static final List V6_ARRAY_KEYWORDS = keywords("items", "additionalItems", "minItems",
            "maxItems", "uniqueItems", "contains");

    private static final List V4_OBJECT_KEYWORDS = keywords("properties", "required",
            "minProperties",
            "maxProperties",
            "dependencies",
            "patternProperties",
            "additionalProperties");

    private static final List V4_ARRAY_KEYWORDS = keywords("items", "additionalItems", "minItems",
            "maxItems", "uniqueItems");

    private static List keywords(String... keywords) {
        return unmodifiableList(asList(keywords));
    }

    private static final Map V4_VALIDATORS = formatValidators(null,
            new DateTimeFormatValidator(),
            new URIV4FormatValidator(),
            new EmailFormatValidator(),
            new IPV4Validator(),
            new IPV6Validator(),
            new HostnameFormatValidator()
    );

    private static final Map V6_VALIDATORS = formatValidators(V4_VALIDATORS,
            new JsonPointerFormatValidator(),
            new URIFormatValidator(),
            new URIReferenceFormatValidator(),
            new URITemplateFormatValidator()
    );

    private static final Map V7_VALIDATORS = formatValidators(V6_VALIDATORS,
            new DateFormatValidator(),
            new URIFormatValidator(false),
            new TimeFormatValidator(),
            new RegexFormatValidator(),
            new RelativeJsonPointerFormatValidator()
    );

    private static Map formatValidators(Map parent, FormatValidator... validators) {
        Map validatorMap = (parent == null) ? new HashMap<>() : new HashMap<>(parent);
        for (FormatValidator validator : validators) {
            validatorMap.put(validator.formatName(), validator);
        }
        return unmodifiableMap(validatorMap);
    }

    abstract List arrayKeywords();

    abstract List objectKeywords();

    public abstract String idKeyword();

    abstract List metaSchemaUrls();

    abstract Map defaultFormatValidators();

    public boolean isAtLeast(SpecificationVersion lowerInclusiveBound) {
        return this.ordinal() >= lowerInclusiveBound.ordinal();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy