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
807 stars 320 forks source link

1.4.0: Is there a way to perform local verification? #1015

Closed fitz-97 closed 1 month ago

fitz-97 commented 3 months ago
image

I try to use this project to do some verification as shown in the picture. This schema is provided by Amazon, but its $id and $schema are not reachable URLs, but more like an identifier, nothing more. But when I try to build JsonSchema, it seems that I will actively go to this URL to get the corresponding structure. This is not what I expected. I hope to get the Schema directly from the file or String instead of through the network protocol. I tried looking for "examples" but couldn't find any. If there is a solution, please help me, thank you

justin-tay commented 3 months ago

Examples can be found at https://github.com/networknt/json-schema-validator/blob/master/doc/schema-retrieval.md.

fitz-97 commented 3 months ago

Examples can be found at https://github.com/networknt/json-schema-validator/blob/master/doc/schema-retrieval.md.

thank u for ur attention, But I couldn't solve it, it still verifies my $schema or $id. Will you consider using a simpler way to support this requirement? Because of performance issues, I still want to use this project

justin-tay commented 3 months ago

You at least need to post what you have tried and what isn't working.

justin-tay commented 3 months ago

The following is an example since you didn't post what are the contents of the schema or the meta-schema. Typically you will map the schema data from a classpath resource so that you don't have it all as an inline string.

        String metaSchemaData = "{}";
        String schemaData = "{}";

        Map<String, String> schemas = new HashMap<>();
        schemas.put("https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1", metaSchemaData);
        schemas.put("https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/HAIR_BRUSH", schemaData);

        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909,
                builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(schemas)));
        JsonSchema schema = factory.getSchema(SchemaLocation
                .of("https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/HAIR_BRUSH"));
        String inputData = "{}";
        System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL));

It looks like there is a custom meta-schema https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1 with a custom vocabulary and custom keywords. If you need those keywords to do anything you will have to implement them yourself as they are non-standard.

fitz-97 commented 3 months ago

The following is an example since you didn't post what are the contents of the schema or the meta-schema. Typically you will map the schema data from a classpath resource so that you don't have it all as an inline string.

        String metaSchemaData = "{}";
        String schemaData = "{}";

        Map<String, String> schemas = new HashMap<>();
        schemas.put("https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1", metaSchemaData);
        schemas.put("https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/HAIR_BRUSH", schemaData);

        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V201909,
                builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(schemas)));
        JsonSchema schema = factory.getSchema(SchemaLocation
                .of("https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/HAIR_BRUSH"));
        String inputData = "{}";
        System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL));

It looks like there is a custom meta-schema https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1 with a custom vocabulary and custom keywords. If you need those keywords to do anything you will have to implement them yourself as they are non-standard.

I seem to have found a solution, thank you for your answer