Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/it/allowed-trailing-comma/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult = success
44 changes: 44 additions & 0 deletions src/it/allowed-trailing-comma/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.sylvainlaurent.maven.it</groupId>
<artifactId>validate-it</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<allowTrailingComma>true</allowTrailingComma>
<validationSets>
<validationSet>
<includes>
<include>src/main/resources/*.yml</include>
<include>src/main/resources/*.json</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"list": [1, 2,],
"second": 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
root:
list: [1, 2,]
dict: { value: key, value2: key2, }
1 change: 1 addition & 0 deletions src/it/duplicate-keys-json/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult = failure
43 changes: 43 additions & 0 deletions src/it/duplicate-keys-json/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.sylvainlaurent.maven.it</groupId>
<artifactId>validate-it</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<detectDuplicateKeys>true</detectDuplicateKeys>
<validationSets>
<validationSet>
<includes>
<include>src/main/resources/*.json</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"value": "XYZ",
"id": 2
}
1 change: 1 addition & 0 deletions src/it/duplicate-keys-yaml/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult = failure
43 changes: 43 additions & 0 deletions src/it/duplicate-keys-yaml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.sylvainlaurent.maven.it</groupId>
<artifactId>validate-it</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<detectDuplicateKeys>true</detectDuplicateKeys>
<validationSets>
<validationSet>
<includes>
<include>src/main/resources/*.yml</include>
</includes>
</validationSet>
</validationSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
item:
id: 1
value: XYZ
id: 2

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# this is an example of the Uber API
# as a demonstration of an API spec in YAML
swagger: '2.0'
# Intentional duplicate here to make sure it passes validation
# if detection of duplicate keys is not enabled:
swagger: '2.0'
info:
title: Uber API
description: Move your app forward with the Uber API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public class ValidateMojo extends AbstractMojo {
@Parameter(name = "allowEmptyFiles", defaultValue = "false")
private boolean allowEmptyFiles;

/**
* Set to <code>true</code> to detect duplicate keys in JSON and YAML dictionaries.
*/
@Parameter(defaultValue = "false")
private boolean detectDuplicateKeys;

/**
* Set to <code>true</code> to allow for single trailing comma following final value or member.
*/
@Parameter(defaultValue = "false")
private boolean allowTrailingComma;

@Override
public void execute() throws MojoExecutionException {
boolean encounteredError = false;
Expand All @@ -63,7 +75,7 @@ public void execute() throws MojoExecutionException {

for (final ValidationSet set : validationSets) {
final ValidationService validationService = new ValidationService(set.getJsonSchema(),
allowEmptyFiles);
allowEmptyFiles, detectDuplicateKeys, allowTrailingComma);

final File[] files = set.getFiles(basedir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
Expand All @@ -22,13 +23,22 @@ public class ValidationService {

private final boolean isEmptyFileAllowed;

public ValidationService(final File schemaFile, final boolean isEmptyFileAllowed) {
public ValidationService(final File schemaFile, final boolean isEmptyFileAllowed,
final boolean detectDuplicateKeys, final boolean allowTrailingComma) {
schema = getJsonSchema(schemaFile);
this.isEmptyFileAllowed = isEmptyFileAllowed;
if (detectDuplicateKeys) {
this.jsonMapper.enable(Feature.STRICT_DUPLICATE_DETECTION);
this.yamlMapper.enable(Feature.STRICT_DUPLICATE_DETECTION);
}
if (allowTrailingComma) {
this.jsonMapper.enable(Feature.ALLOW_TRAILING_COMMA);
this.yamlMapper.enable(Feature.ALLOW_TRAILING_COMMA);
}
}

public ValidationService(final File schemaFile) {
this(schemaFile, false);
this(schemaFile, false, false, false);
}

public ValidationResult validate(final File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.junit.Assert.*;

public class ValidationServiceAllowEmptyFilesTest {
private ValidationService service = new ValidationService(null, true);
private ValidationService service = new ValidationService(null, true, false, false);

@Test
public void test_empty_file_yml() {
Expand Down