GithubHelp home page GithubHelp logo

alenkacz / gradle-json-validator Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 8.0 113 KB

Gradle plugin for json validation

License: MIT License

Groovy 99.15% Dockerfile 0.85%
json-validation gradle json-schema json-schema-validator

gradle-json-validator's Introduction

gradle-json-validator

Build Status Download

Provides json validation as a part of your gradle build pipeline.

This plugin implements a custom task type, ValidateJsonTask. This task expects two properties - targetJsonFile and jsonSchemaFile (instead of targetJsonFile you can use targetJsonDirectory and then all files in that directory will be validated). If that directory contains both json and non-json files, the task will fail for non-json files. If you want to validate only files with .json extension use the onlyWithJsonExtension property. If you need to validate more jsons as a part of one build, you will have to create as many custom tasks as the number of json schema files (see validateCustomJson in the example below).

Usage

buildscript {
	repositories {
		jcenter()
	}
	dependencies {
		classpath 'cz.alenkacz.gradle:json-validator:FILL_VERSION_HERE'
	}
}

apply plugin: 'json-validator'

import cz.alenkacz.gradle.jsonvalidator.ValidateJsonTask

task validateCustomJson(type: ValidateJsonTask) {
  targetJsonFile = file("target.json") // only one of targetJsonFile or targetJsonDirectory can be specified 
  targetJsonDirectory = file("directoryWithJsons") // only one of targetJsonFile or targetJsonDirectory can be specified
  jsonSchema = file("schema.json")
  onlyWithJsonExtension = true // default is false, this makes sense only when using targetJsonDirectory, it ignores all files apart from those who do not have .json file extension
}

JSON schema syntax check

For some build pipelines it might be useful to be able to check schema files for syntax errors as a part of the build. To make that work, use the following setup and run the task validateJsonSchema:

buildscript {
	repositories {
		jcenter()
	}
	dependencies {
		classpath 'cz.alenkacz.gradle:json-validator:FILL_VERSION_HERE'
	}
}

apply plugin: 'json-validator'

jsonSchema {
    schemaFolder = PATH_TO_YOUR_FOLDER_WITH_JSON_SCHEMAS
}

gradle-json-validator's People

Contributors

alenkacz avatar lnarita avatar timyates avatar wyhasany avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gradle-json-validator's Issues

Possibility to validate multiple files

Hey there,
is there any possibility to validate multiple files? E.g. by giving the validator a directory or expression with wildcards?

Regards
Marius

Validation does not work for schemas with JSONArray as top level

Problem occurs here, because top level object is JSONArray instead of JSONObject:

schema.validate(new JSONObject(new FileInputStream(it).getText()))

Example schema:

{
    "$schema": "http://json-schema.org/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "country": {
                "type": "string"
            }
        },
        "required": [
            "country"
        ]
    }
}

Deprecation warnings in Gradle 6

Running ./gradlew test --warning-mode on a project using this plugin with Gradle result outputs the following warnings:

> Task :validateJsonSchema
Property 'pluginExtension' is not annotated with an input or output annotation. This behaviour has been deprecated and is scheduled to be removed in Gradle 7.0.

This is coming out of the APT plugin: https://github.com/tbroyer/gradle-apt-plugin

This deprecation is caused by new checks Gradle is doing in 6.0 to identify missing inputs to a task.

If statements are not correctly parsed

The attached schema and json, using an if/then statement to enforce a restricted return as a build successful, when in jsonschemalint.com it correctly returns as [2].domain | should be equal to one of the allowed values | "internal.example.com" -- | -- | --

ingest.json

[
	{
		"domain": "example.com",
		"platform": "linux"
	},
	{
		"domain": "example.com",
		"platform": "windows"
	},
	{
		"domain": "internal.example.com",
		"platform": "windows"
	}
]

ingest.schema.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "minItems": 1,
  "uniqueItems": true,
  "items": {
    "required": ["domain", "platform"],
    "properties": {
      "domain": {
        "enum": ["example.com", "internal.example.com"]
      },
      "platform": {
        "enum": ["linux", "windows" ]
      }
    },
    "if": {
      "properties": {
        "platform": {
          "enum": ["windows"]
        }
      }
    }, "then": {
      "properties": {
        "domain": {
          "enum": ["example.com"]
        }
      }
    }
  }
}

build.gradle

import groovy.json.JsonSlurper

defaultTasks 'groupByPlatform'

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.cz.alenkacz.gradle:json-validator:1.2.1"
  }
}

apply plugin: "cz.alenkacz.gradle.jsonvalidator"


ext {
  targetJsonFileame = "ingest.json"
  jsonSchemaFilename = "ingest.schema.json"
}

task validateInputJson(type: cz.alenkacz.gradle.jsonvalidator.ValidateJsonTask) {
  description = "Validates JSON input against the JSON schema."
  group = "JSON Parsing"
  targetJsonFile = file(targetJsonFileame)
  jsonSchema = file(jsonSchemaFilename)
}

Output:

gradlew.bat validateInputJson

BUILD SUCCESSFUL in 32s
1 actionable task: 1 executed

gradle --version

X:\Tools\gradlew\gradlew.bat --version

------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------

Build time:   2018-09-19 18:10:15 UTC
Revision:     b4d8d5d170bb4ba516e88d7fe5647e2323d791dd

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_151 (Oracle Corporation 25.151-b12)
OS:           Windows 7 6.1 amd64

Should provide better validation output

Thanks for this great plugin.
I ran it on a test JSON file and it produces only this output:

:validateCatalog FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':validateCatalog'.
> #: 7 schema violations found


That is better then nothing, but frustrating saying there are '7 schema violations' but not any indication of what they are, line #, object, property nothing ...
Even the filename would be nice :)

How does the validator choose which files to validate?

If I want to validate ALL the jsons in the project, I can provide the root directory. But it's not clear from the README how will the validator identify the files to validate? will it scan for *.json files? or any file that is in the directory will be validated?

Please provide some description of how to use this plugin

I am looking for a way to validate JSON as part of a Gradle build and found your project. It looks quite promision, but I could not found any description and I don't know much about Gradle, could you add a short piece of description/documentation so others can make use of your work here?

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.