Git Product home page Git Product logo

Comments (4)

justin-tay avatar justin-tay commented on September 20, 2024

To support custom vocabularies, for V201909 and V202012 the keywords are moved into their respective vocabularies.

"https://json-schema.org/draft/2020-12/vocab/validation", ValidatorTypeCode.TYPE, ValidatorTypeCode.CONST,

I cannot replicate your issue so you will likely need to supply more information about what you are doing

package com.networknt.schema;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.serialization.JsonMapperFactory;

public class Issue994Test {
    @Test
    void test() throws JsonMappingException, JsonProcessingException {
        String schemaData = "{\r\n"
                + "    \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n"
                + "    \"type\": \"object\",\r\n"
                + "    \"properties\": {\r\n"
                + "        \"textValue\": {\r\n"
                + "            \"type\": [\r\n"
                + "                \"string\",\r\n"
                + "                \"null\"\r\n"
                + "            ],\r\n"
                + "            \"isMandatory\": true\r\n"
                + "        }\r\n"
                + "    }\r\n"
                + "}";
        JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(schemaData);
        JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaNode);
        String inputData = "{\r\n"
                + "  \"textValue\": \"hello\"\r\n"
                + "}";
        System.out.println(schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL));
    }
}

from json-schema-validator.

duke4 avatar duke4 commented on September 20, 2024

The hint with the vocabularies made my code working 👍

To give more context:
I build a schema factory with a custom meta schema like this now

// base on JsonMetaSchema.V202012 copy code below
JsonMetaSchema customMetaSchema = new JsonMetaSchema.Builder("https://json-schema.org/draft/2020-12/schema")
    .idKeyword("$id")
    .formats(Formats.DEFAULT)
    .keywords(ValidatorTypeCode.getKeywords(SpecVersion.VersionFlag.V202012))
    .keywords(Vocabulary.V202012_VALIDATION.getKeywords())
    // keywords that may validly exist, but have no validation aspect to them
    .keywords(Arrays.asList(
        new NonValidationKeyword("$schema"),
        new NonValidationKeyword("$id"),
        new NonValidationKeyword("title"),
        new NonValidationKeyword("description"),
        new NonValidationKeyword("default"),
        new NonValidationKeyword("definitions"),
        new NonValidationKeyword("$comment"),
        new NonValidationKeyword("$defs"),
        new NonValidationKeyword("$anchor"),
        new NonValidationKeyword("deprecated"),
        new NonValidationKeyword("contentMediaType"),
        new NonValidationKeyword("contentEncoding"),
        new NonValidationKeyword("examples"),
        new NonValidationKeyword("then"),
        new NonValidationKeyword("else"),
        new NonValidationKeyword("additionalItems")
    ))
    // add your custom keyword
    .keyword(new IsMandatoryKeyword())
    .build();

JsonSchemaFactory jsonSchemaFactory = new JsonSchemaFactory.Builder().defaultMetaSchemaIri(customMetaSchema.getIri())
    .metaSchema(customMetaSchema)
    .build();

JsonSchema jsonSchema = jsonSchemaFactory.getSchema(jsonNode);

Maybe there is some more room for improvement?

from json-schema-validator.

justin-tay avatar justin-tay commented on September 20, 2024

The JsonMetaSchema builder accepts a JsonMetaSchema instance as a base if you want to modify an existing one.

JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012()).keyword(new IsMandatoryKeyword())).build();

from json-schema-validator.

duke4 avatar duke4 commented on September 20, 2024

Thank you. This works very well 🥳

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.