Giter VIP home page Giter VIP logo

json-schema-validation-starter's Introduction

Json-Schema-Validation-Starter

This provides a Spring-Boot-Starter to include JsonSchemaValidation with the help of the https://github.com/networknt/json-schema-validator -library.

Foo

Usage

Include the starter into you're project.

You need to add jitpack to your pom.xml because this project is not available in the official maven repository.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Note: For Spring Boot < 3.0 please use 2.3.0, otherwise use 3.x.x for Spring Boot >= 3.0.

Add the json-schema-validation-starter-dependency to your pom.xml

<dependency>
    <groupId>com.github.JanLoebel</groupId>
    <artifactId>json-schema-validation-starter</artifactId>
    <version>3.0.1</version>
</dependency>

After that simply create a json-schema and put it into e.g.: resources/jsonschema/book.json. The last step is now to let your entity know that it should be validated and which schema it should use.

@JsonSchemaValidation("classpath:jsonschema/book.json")
public class Book {
    private String title;
    private String author;
}

Alternatively, you need to add this annotation in your controller like below. This is helpful when you generate your classes from a schema or can't edit them.

@RestController
@RequestMapping("/books")
public BooksConroller {
     @PostMapping
     public ResponseEntity<Book> createBook(@RequestBody @JsonSchemaValidation("classpath:jsonschema/book.json") Book bookDto) {
        //...
        return bookDto;
    }
}

The schemas are read by a bean that implements the JsonSchemaProvider interface. By default, the DefaultJsonSchemaProvider is used, which can load schemas by a URL or from the classpath. If necessary, a custom schema provider can be implemented and configured:

@ConditionalOnProperty(prefix = "json.schema.validation", name = "schemaProvider", havingValue = "custom")
@Component
public class CustomJsonSchemaProvider implements JsonSchemaProvider {
     JsonSchema loadSchema(String url) {
         // Create and return a JSON schema...
     }

    void handleValidationMessages(Collection<ValidationMessage> validationMessages) {
         // Handle validation messages...
    }
}

Configure the schema provider in application.properties:

json.schema.validation.schemaProvider=custom

Example project

Head over to http://github.com/JanLoebel/json-schema-validation-starter-example to checkout the sample project.

Contribution

Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)

License

MIT License

Copyright (c) 2019 Jan Löbel

See LICENSE file for details.

json-schema-validation-starter's People

Contributors

dependabot[bot] avatar janloebel avatar jochenberger avatar murygin avatar yasokada-lc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json-schema-validation-starter's Issues

Application not loading due to lack of Bean

Receiving the message:

Consider defining a bean of type 'com.github.JanLoebel.jsonschemavalidation.provider.JsonSchemaProvider' in your configuration.

Application fails to start and basic context loading test fails.

Using @ComponentScan or @EntityScan pointing to the package doesn't fix the issue.

Using:

'org.springframework.boot' version '2.7.12'
'com.networknt:json-schema-validator:1.0.82'
'com.github.JanLoebel:json-schema-validation-starter:2.3.0'

Does not cover the object correctly when there are multiple refractions

Hi,

I have two sub relation and doesn't map from schema to object.

Schema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "data": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string"
                },
                "id": {
                    "type": "string"
                },
                "attributes": {
                    "type": "object",
                    "properties": {
                        "title": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "title"
                    ]
                }
            },
            "required": [
                "type",
                "id"
            ]
        }
    },
    "required": [
        "data"
    ]
}

DTO

@JsonSchemaValidation("book.json")
public class BookDto {
    
    private String title;
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Result:
{ "title": null }

Run tests on CI

Could you please setup simple GitHub CI task to run tests on each PR?

Auto-Configuration does not work with Spring Boot 3

Auto-configuration must be done via META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports now.

Rather than a single comma-separate list, each line contains the fully qualified name of an auto-configuration class.

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#changes-to-auto-configuration
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#auto-configuration-files

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.