Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fitz-97 opened this issue Apr 11, 2024 · 5 comments
Closed

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

fitz-97 opened this issue Apr 11, 2024 · 5 comments

Comments

@fitz-97
Copy link

fitz-97 commented Apr 11, 2024

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
Copy link
Contributor

@fitz-97
Copy link
Author

fitz-97 commented Apr 12, 2024

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
Copy link
Contributor

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

@justin-tay
Copy link
Contributor

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
Copy link
Author

fitz-97 commented Apr 15, 2024

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

@stevehu stevehu closed this as completed May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants