networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
858 stars 325 forks source link

Walking over JSON Schema and JDK upgradation to 8 or above. #316

Closed prashanthjos closed 1 year ago

prashanthjos commented 4 years ago

We have a requirement where we want to walk through the JSON Schema and corresponding JSON data node properties at the same time the way we do with validator's today. For example the Validate method on MaxItemsValidator allows us to validate the JSON data node for a specific Schema Node .

public MaxItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
        super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MAX_ITEMS, validationContext);
        if (schemaNode.isIntegralNumber()) {
            max = schemaNode.intValue();
        }

        parseErrorCode(getValidatorType().getErrorCodeKey());
    }

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
        debug(logger, node, rootNode, at);

        if (node.isArray()) {
            if (node.size() > max) {
                return Collections.singleton(buildValidationMessage(at, "" + max));
            }
        } else if (config.isTypeLoose()) {
            if (1 > max) {
                return Collections.singleton(buildValidationMessage(at, "" + max));
            }
        }

        return Collections.emptySet();
    }

Taking this to a more generic level can we have a walk or visit method on the validator's that allows us to do more custom logic's beyond validations.

public void visit(Collection<JsonValidator> visitors, JsonNode node);

While walking through the schema we want to collect different kinds of information co-relating to the localized node and schema data. We use this library for validating our schema's today and I think we should also make the library more robust and useful by introducing the schema walkers or visitors.

There is a library in javascript which does something on these lines. https://github.com/cloudflare/json-schema-tools/tree/master/workspaces/json-schema-walker

Also can we move this library to JDK 8 or beyond.

stevehu commented 4 years ago

@prashanthjos I think we have discussed the visiting feature in another issue and you have submitted a PR to implement something already. Are you talking about making another round of enhancements? I am open to it.

https://github.com/networknt/json-schema-validator/issues/249

Currently, there are some users who are stuck on the older version of Andriod which requires Java 6. We don't want to leave those users behind, so let's keep Java 6 for now until everyone is moved to Java 8 and higher. In the parent light platform, we are even considering to discard Java 8 and focus on Java 11 and above for the cloud-native support.

prashanthjos commented 4 years ago

The one I mentioned in #249 was collecting data while validation is happening, for this issue we want to walk through the schema and data without validating and just for collecting or recording information. I want to introduce a visit or walk method that my validators can implement and do things beyond validation. Please let me know your thoughts.

stevehu commented 4 years ago

As long as there is no performance impact, I am interested in the feature. Are you planning to inject some logic to each individual validator or just add an interception in the base validator?

prashanthjos commented 4 years ago

We will just have a new method in each validator named visit or walk. This has no impact on the current validation logics we have. In fact I wanted to define a default method on JsonValidator interface but default methods are available from Java 8. Is it possible for us to cut a new version indicating moving to java 8 or later? We are missing out on many new features which makes code more optimal and maintainable.

stevehu commented 4 years ago

The approach is nice and clean. So many people have tried to move to Java 8 in the past but eventually, we have to downgrade back to Java 6 as there are still some users depending on it for the new features. I know the pain of stuck in Java 6 but those Andriod developers are early adopters and we have to support them. Most Java 8 features can be done in Java 6 with some tricks although the code is not clean and optimal. Let's put some effort to make everybody happy for now and prepare to move to Java 8 once the roadblocks are gone. Thanks.

matejsp commented 4 years ago

Currently jackson requires java 1.7. So running on java 1.6 ...

https://repo1.maven.org/maven2/com/fasterxml/jackson/jackson-parent/2.10/jackson-parent-2.10.pom https://medium.com/@cowtowncoder/jackson-2-10-features-cd880674d8a2

stevehu commented 4 years ago

Thanks a lot for the links. People who are using Java 6 can overwrite the Jackson version to the right one.

matejsp commented 4 years ago

Hm ... Android you say ...

Android: https://source.android.com/setup/build/older-versions Play requirements: https://developer.android.com/distribute/play-policies

For example 64 bit support is required since 2019. And 64 bit is from android 5.0 (using java 7).

I wonder how they can even publish on play with such old tooling (since you must target at least android 9 api).

stevehu commented 4 years ago

Regarding to the Andriod support, there are some discussions on this thread. https://github.com/networknt/json-schema-validator/issues/221

I would like to reopen it to discuss so that we can move on to Java 8. It is a burden for all of us to support the obsoleted version of Android API.

stevehu commented 4 years ago

I haven't heard any response from another thread. I think we can safely move to Java 8; however, let's don't change the codebase for now in case we need to rollback. I would just change the pom.xml for the source and target version. What do you think?

matejsp commented 4 years ago

Just bump it. Even android developers can use java 8 api for all devices.

Very interesting read: https://medium.com/androiddevelopers/support-for-newer-java-language-apis-bca79fc8ef65 https://developer.android.com/studio/write/java8-support

stevehu commented 4 years ago

@matejsp Thanks a lot for the links. I am not familiar with Andriod development in Java. I only explored a little bit with React Native before. Good to know we can safely move on.

fdutton commented 1 year ago

This was resolved in pull-request #336