Giter VIP home page Giter VIP logo

Comments (5)

justin-tay avatar justin-tay commented on May 27, 2024 1

That actually depends on what you define to be a valid schema.

You would typically perform validation of a schema by using it's meta-schema. This only validates it's structure though, for instance for a $ref it would only just check if it is a valid uri_reference format and to figure out if the $ref can resolve, you would need to actually load the schema.

Here however, from a meta-schema perspective, your example schema is valid, as it would allow unknown keywords. You may consider a custom meta-schema eg. using unevaluatedProperties: false or something to that effect or you could add extra restrictions when loading the schema.

The following validates the schema according to the meta-schema, and it's valid due to how the meta-schema is defined.

package com.networknt.schema;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

public class Issue996Test {
    @Test
    void metaSchemaValidation() {
        String instanceData = "{\r\n"
                + "    \"lol\": 420\r\n"
                + "}";
        SchemaValidatorsConfig config = new SchemaValidatorsConfig();
        config.setPathType(PathType.JSON_POINTER);
        config.setFormatAssertionsEnabled(true);
        JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(SchemaLocation.of(SchemaId.V202012), config);
        System.out.println(schema.validate(instanceData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> {
            executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true);
            executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true);
        }));
    }
}

You can also load the schema with additional restrictions after validating against the meta-schema. This for instance doesn't allow unknown keywords or unknown formats. You need to initializeValidators to check if all the $ref resolve.

package com.networknt.schema;

import org.junit.jupiter.api.Test;

import com.networknt.schema.SpecVersion.VersionFlag;

public class Issue996Test {
    @Test
    void test() {
        String instanceData = "{\r\n"
                + "    \"lol\": 420\r\n"
                + "}";
        JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012())
                .unknownKeywordFactory(DisallowUnknownKeywordFactory.getInstance())
                .build();
        SchemaValidatorsConfig config = new SchemaValidatorsConfig();
        config.setStrict("format", true);
        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.metaSchema(metaSchema));
        JsonSchema schema = factory.getSchema(instanceData, config);
        schema.initializeValidators();
    }
}

from json-schema-validator.

justin-tay avatar justin-tay commented on May 27, 2024 1

That's not really possible with the current code. It's not really designed as a linter. You can send a PR if you think you can add this without affecting the validation performance.

There are many reasons why a schema may be potentially invalid. According to the spec, unknown keywords are completely acceptable, they are just treated as annotation keywords, so you can see that what is valid depends on interpretation. You might also want to restrict the schemas to only use particular draft versions also for instance.

You would likely need to design a custom meta-schema implementation with custom validators if you want to do this. You would need custom validators because JSON Schema actually doesn't have built in validators for doing validation of data, for instance if you want to validate if minProperties is always less than maxProperties that can't be done with any standard keyword. Also validating whether a $ref is resolvable is non-trivial without doing the load.

from json-schema-validator.

jksevend-trimble avatar jksevend-trimble commented on May 27, 2024

This is really helpful @justin-tay
Thank you very much. I will try it

from json-schema-validator.

jksevend-trimble avatar jksevend-trimble commented on May 27, 2024

Hello again,
Your second solution is really great. I can catch the exception and retrieve a message like

Keyword '$ids' is unknown and must be configured on the meta-schema or vocabulary.

Now I do not want to get picky here, but in case of multiple typos like "$ids" and "$schemas" I would only get the first key which is not according to the meta schema. Could there be a way to "collect" all errors?

from json-schema-validator.

jksevend-trimble avatar jksevend-trimble commented on May 27, 2024

Okay, I think this is not really a big issue for us now. It is okay as it is. I just wanted to actively ask.

Thank you for all your help!

from json-schema-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.