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

com.dimajix.shaded.everit.schema.internal.JsonPointerFormatValidator 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.internal;

import static java.lang.String.format;

import java.util.Optional;

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

public class JsonPointerFormatValidator implements FormatValidator {

    @Override public Optional validate(String subject) {
        if ("".equals(subject)) {
            return Optional.empty();
        }
        try {
            new JSONPointer(subject);
            if (subject.startsWith("#")) {
                return failure(subject);
            }
            return checkEscaping(subject);
        } catch (IllegalArgumentException e) {
            return failure(subject);
        }
    }

    protected Optional failure(String subject) {
        return Optional.of(format("[%s] is not a valid JSON pointer", subject));
    }

    protected Optional checkEscaping(String subject) {
        for (int i = 0; i < subject.length() - 1; ++i) {
            char c = subject.charAt(i);
            if (c == '~') {
                char next = subject.charAt(i + 1);
                if (next == '1' || next == '0') {
                    continue;
                }
                return failure(subject);
            }
        }
        if (subject.charAt(subject.length() - 1) == '~') {
            return failure(subject);
        }
        return Optional.empty();
    }

    @Override public String formatName() {
        return "json-pointer";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy