A JSON schema validator

A JSON schema validator

A simple JSON schema validator for the Vert.x world.

This JSON validator is well adapted to be used in conjunction with Vert.x.

At the moment it supports Draft 4, Draft 6 or Draft 7, and Java8+.

The code can be found here: https://github.com/ersh112356/vertx-json-validator

A background:

In the new world of Microservices that domains modern systems these days, JSON is an essential part. Requests and responses are all built around JSON. So there’s a clear need to validate the request against known schema. That’s to avoid many security hazards. Indeed, many implementations can be found out there, for my best knowledge, none is well adapted to the world of Vert.x.

That’s my humble contribution. It works on production, yet use it on your own risk.

When to use this:

There are a few good JSON schema validators out there, I was looking for a decent one that can integrate against Vert.x, yet ended empty handed.

This one is simple to integrate and quite fast.

The code includes a few samples of usage, so you just need to put the dependencies into your classpath, write a few simple lines of code, and that’s it!

Depedencies:

vertx-core-3.x

jackson-annotations-2.9.8

jackson-core-2.9.8

jackson-databind-2.9.8

netty-buffer-4.1.30.Final

accessors-smart-1.2

asm-5.0.4

json-path-2.4.0

json-smart-2.3

slf4j-api-1.7.25

vertx-response (optional)


The code can be found here https://github.com/ersh112356/vertx-json-validator.


A quickstart:

String goodObjectJson_IV = "{\n" +

" \"fruits\": [ \"apple\", \"orange\", \"pear\" ],\n" +

" \"vegetables\": [\n" +

" {\n" +

" \"veggieName\": \"potato\",\n" +

" \"veggieLike\": true\n" +

" },\n" +

" {\n" +

" \"veggieName\": \"broccoli\",\n" +

" \"veggieLike\": false\n" +

" }\n" +

" ]\n" +

"}";




String schemaObjectData_IV = "{\n" +

" \"$id\": \"https://example.com/arrays.schema.json\",\n" +

" \"$schema\": \"https://json-schema.org/draft-07/schema#\",\n" +

" \"description\": \"A representation of a person, company, organization, or place\",\n" +

" \"type\": \"object\",\n" +

" \"properties\": {\n" +

" \"fruits\": {\n" +

" \"type\": \"array\",\n" +

" \"items\": {\n" +

" \"type\": \"string\"\n" +

" }\n" +

" },\n" +

" \"vegetables\": {\n" +

" \"type\": \"array\",\n" +

" \"items\": { \"$ref\": \"#/definitions/veggie\" }\n" +

" }\n" +

" },\n" +

" \"definitions\": {\n" +

" \"veggie\": {\n" +

" \"type\": \"object\",\n" +

" \"required\": [ \"veggieName\", \"veggieLike\" ],\n" +

" \"properties\": {\n" +

" \"veggieName\": {\n" +

" \"type\": \"string\",\n" +

" \"description\": \"The name of the vegetable.\"\n" +

" },\n" +

" \"veggieLike\": {\n" +

" \"type\": \"boolean\",\n" +

" \"description\": \"Do I like this vegetable?\"\n" +

" }\n" +

" }\n" +

" }\n" +

" }\n" +

"}";


schema = new JsonObject(schemaObjectData_IV);

sc = JsonSchemaResolver.resolveSchema(schema.getMap());

Result result = JsonSchema.conformsSchema(new JsonObject(goodObjectJson_IV),sc);

assertTrue(result.result());

Have fun!

Elad Yosifon

3x Profitable Startup CTO | 1 Exit | Angel Investor | Mentor | Consultant

5 年

Why do you think other JSON validators are not playing nice with Vert.x?

要查看或添加评论,请登录

Eran Shaham的更多文章

  • Microservices Chatbot and Coronavirus

    Microservices Chatbot and Coronavirus

    A few weeks ago I shared a short post about a new initiative of mine to have a fun bot to make life much easier in…

  • Docker image build vs. jib

    Docker image build vs. jib

    Jib is an open-source Java containerizer originally coming from Google. Jib allows to build Docker images from Java…

  • vertx-lucene-classification

    vertx-lucene-classification

    Lucene is here for a long time, ML was added to Lucene for a few releases now, yet some aspects were left out. ML can…

  • Kafka vs. Pulsar

    Kafka vs. Pulsar

    Kafka is here for a long time. Perhaps too long.

    1 条评论
  • UMLet- an open source UML tool

    UMLet- an open source UML tool

    Some aspects of my day job work are drawing many diagrams. That's part of an architect role to create design documents…

    2 条评论
  • Revive- a Single Page Application framework

    Revive- a Single Page Application framework

    I'm uploading a short presentation about a new open sourced Revive which I've made public. Revive is a new light open…

  • A few words on Docker and Kubernetes

    A few words on Docker and Kubernetes

    We all know Docker Engine; it’s a container runtime. We can run “docker run” on a host whether it’s a server or a VM…

    2 条评论
  • A poor man Dependency Injection

    A poor man Dependency Injection

    Dependency Injection (DI) has been around for a while now. A typical use case would be, for instance, the same piece of…

  • Apache Storm and big data

    Apache Storm and big data

    A background: Big data is here for a while now. At the practical level, big data helps us to better understand our…

  • Cassandra VS. MongoDB

    Cassandra VS. MongoDB

    Cassandra and MongoDB became to be the two of the most popular NOSQL databases that are running around in the last few…

    4 条评论

社区洞察

其他会员也浏览了