java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.62k stars 399 forks source link

New KeywordValidator not getting invoked by the component #202

Open santoshgangavaram opened 8 years ago

santoshgangavaram commented 8 years ago

Please find the schema as below and the code i have used from Example9 to register all the objects as below. I am not able to figure out what is wrong in this case. Please help. Thanks.

schema.json

{ "title": "Product", "description": "A product from Acme's catalog", "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer"

    },
    "name": {
        "description": "Name of the product",
        "type": "string"
    },
    "price": {
        "type": "number",
        "minimum": 0,
        "validwhen" : "name != Y"
    },
    "tags": {
        "type": "array",
        "items": {
            "type": "string"
        },
        "minItems": 1,
        "uniqueItems": true
    }
},
"required": ["id", "name", "price"]

}

data.json

{ "id": 1, "name": "A green door", "price": 12.50, "tags": ["home", "green"] }

import java.io.IOException; import java.util.Collection;

import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jackson.JsonLoader; import com.github.fge.jackson.NodeType; import com.github.fge.jackson.jsonpointer.JsonPointer; import com.github.fge.jsonschema.cfg.ValidationConfiguration; import com.github.fge.jsonschema.core.exceptions.ProcessingException; import com.github.fge.jsonschema.core.keyword.syntax.checkers.AbstractSyntaxChecker; import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker; import com.github.fge.jsonschema.core.processing.Processor; import com.github.fge.jsonschema.core.report.ProcessingReport; import com.github.fge.jsonschema.core.tree.SchemaTree; import com.github.fge.jsonschema.examples.Example8; import com.github.fge.jsonschema.keyword.digest.Digester; import com.github.fge.jsonschema.keyword.digest.helpers.IdentityDigester; import com.github.fge.jsonschema.keyword.digest.helpers.SimpleDigester; import com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator; import com.github.fge.jsonschema.keyword.validator.KeywordValidator; import com.github.fge.jsonschema.library.DraftV4Library; import com.github.fge.jsonschema.library.Keyword; import com.github.fge.jsonschema.library.KeywordBuilder; import com.github.fge.jsonschema.library.Library; import com.github.fge.jsonschema.library.LibraryBuilder; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaFactory; import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; import com.github.fge.jsonschema.processors.data.FullData; import com.github.fge.msgsimple.bundle.MessageBundle; import com.github.fge.msgsimple.load.MessageBundles; import com.github.fge.msgsimple.source.MapMessageSource; import com.github.fge.msgsimple.source.MessageSource;

public final class Example9 { public static void main(final String... args) throws IOException, ProcessingException { final JsonNode customSchema = JsonLoader.fromResource("/schema.json"); final JsonNode good = JsonLoader.fromResource("/data.json"); //final JsonNode bad = Utils.loadResource("/custom-keyword-bad.json");

    /*
     * Build the new keyword
     */
    final Keyword keyword = Keyword.newBuilder("validwhen")
        .withSyntaxChecker(ValidwhenSyntaxChecker.getInstance())
        .withSimpleDigester(NodeType.STRING, NodeType.NULL)
        .withValidatorClass(ValidwhenKeywordValidator.class).freeze();

    /*
     * Build a library, based on the v4 library, with this new keyword
     */
    final Library library = DraftV4Library.get().thaw()
        .addKeyword(keyword).freeze();

    /*
     * Complement the validation message bundle with a dedicated message
     * for our keyword validator
     */
    final String key = "validwhen";
    final String value = "generic validwhen error message";
    final MessageSource source = MapMessageSource.newBuilder()
        .put(key, value).build();
    final MessageBundle bundle
        = MessageBundles.getBundle(JsonSchemaValidationBundle.class)
        .thaw().appendSource(source).freeze();

    /*
     * Build a custom validation configuration: add our custom library and
     * message bundle
     */
    final ValidationConfiguration cfg = ValidationConfiguration.newBuilder()
        .setDefaultLibrary("http://my.site/myschema#", library)
        .setValidationMessages(bundle).freeze();

    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
        .setValidationConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema(customSchema);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    //report = schema.validate(bad);
    //System.out.println(report);
}

/*
 * Our custom syntax checker
 */
private static final class ValidwhenSyntaxChecker
    extends AbstractSyntaxChecker
{
    private static final SyntaxChecker INSTANCE
        = new ValidwhenSyntaxChecker();

    public static SyntaxChecker getInstance()
    {
        return INSTANCE;
    }

    private ValidwhenSyntaxChecker()
    {
        /*
         * When constructing, the name for the keyword must be provided
         * along with the allowed type for the value (here, an array).
         */
        super("validwhen", NodeType.STRING);
    }

    @Override
    protected void checkValue(final Collection<JsonPointer> pointers,
        final MessageBundle bundle, final ProcessingReport report,
        final SchemaTree tree)
        throws ProcessingException
    {

        final JsonNode node = getNode(tree);

        System.out.println("node ==> "+node);

    }
}

/**
 * Custom keyword validator for {@link Example9}
 *
 * It must be {@code public} because it is built by reflection.
 */
public static final class ValidwhenKeywordValidator
    extends AbstractKeywordValidator
{

    public ValidwhenKeywordValidator(final JsonNode digest)
    {
        super("validwhen");

    }

    @Override
    public void validate(final Processor<FullData, FullData> processor,
        final ProcessingReport report, final MessageBundle bundle,
        final FullData data)
        throws ProcessingException
    {
       System.out.println("data ==> "+data);
       return;
    }

    @Override
    public String toString()
    {
        return "validwhen";
    }
}

}

jlolling commented 7 years ago

I am also interested in a solution for this issue!

mpostelnicu commented 6 years ago

me too. i cannot get a custom validator to validate anything. almost identical situation as the sample code.