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

com.dimajix.shaded.everit.schema.loader.StringSchemaLoader 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 static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
import static com.dimajix.shaded.everit.schema.loader.SpecificationVersion.DRAFT_4;

import java.util.Map;

import com.dimajix.shaded.everit.schema.FormatValidator;
import com.dimajix.shaded.everit.schema.StringSchema;

/**
 * @author erosb
 */
public class StringSchemaLoader {

    private LoadingState ls;

    private Map formatValidators;

    /**
     * Creates an instance with {@link SpecificationVersion#defaultFormatValidators()}  draft v4 format validators}.
     *
     * @deprecated explicitly specify the format validators with {@link #StringSchemaLoader(LoadingState, Map)} instead
     */
    @Deprecated
    public StringSchemaLoader(LoadingState ls) {
        this(ls, DRAFT_4.defaultFormatValidators());
    }

    StringSchemaLoader(LoadingState ls, Map formatValidators) {
        this.ls = requireNonNull(ls, "ls cannot be null");
        this.formatValidators = unmodifiableMap(requireNonNull(formatValidators, "formatValidators cannot be null"));
    }

    public StringSchema.Builder load() {
        StringSchema.Builder builder = StringSchema.builder();
        ls.schemaJson().maybe("minLength").map(JsonValue::requireInteger).ifPresent(builder::minLength);
        ls.schemaJson().maybe("maxLength").map(JsonValue::requireInteger).ifPresent(builder::maxLength);
        ls.schemaJson().maybe("pattern").map(JsonValue::requireString)
                .map(ls.config.regexpFactory::createHandler)
                .ifPresent(builder::pattern);
        ls.schemaJson().maybe("format").map(JsonValue::requireString)
                .ifPresent(format -> addFormatValidator(builder, format));
        return builder;
    }

    private void addFormatValidator(StringSchema.Builder builder, String formatName) {
        FormatValidator formatValidator = formatValidators.get(formatName);
        if (formatValidator != null) {
            builder.formatValidator(formatValidator);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy