Jsonprocessor Utility

Json validator and processor library to validate input request

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.github.salilvnair
ArtifactId

ArtifactId

jsonprocessor
Last Version

Last Version

1.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

Jsonprocessor Utility
Json validator and processor library to validate input request
Project URL

Project URL

https://github.com/salilvnair/jsonprocessor
Source Code Management

Source Code Management

https://github.com/salilvnair/jsonprocessor

Download jsonprocessor

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.salilvnair/jsonprocessor/ -->
<dependency>
    <groupId>com.github.salilvnair</groupId>
    <artifactId>jsonprocessor</artifactId>
    <version>1.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.salilvnair/jsonprocessor/
implementation 'com.github.salilvnair:jsonprocessor:1.0.5'
// https://jarcasting.com/artifacts/com.github.salilvnair/jsonprocessor/
implementation ("com.github.salilvnair:jsonprocessor:1.0.5")
'com.github.salilvnair:jsonprocessor:jar:1.0.5'
<dependency org="com.github.salilvnair" name="jsonprocessor" rev="1.0.5">
  <artifact name="jsonprocessor" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.salilvnair', module='jsonprocessor', version='1.0.5')
)
libraryDependencies += "com.github.salilvnair" % "jsonprocessor" % "1.0.5"
[com.github.salilvnair/jsonprocessor "1.0.5"]

Dependencies

compile (8)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.10.0.pr1
com.google.code.gson : gson jar 2.8.5
org.slf4j : slf4j-api jar 1.7.25
commons-logging : commons-logging jar 1.2
commons-lang : commons-lang jar 2.6
org.apache.commons : commons-lang3 jar 3.8.1
org.apache.commons : commons-collections4 jar 4.4
com.jayway.jsonpath : json-path jar 2.4.0

Project Modules

There are no modules declared in this project.

JSON PROCESSOR

Easy annotation based validator.

Predefined validations right out of the box i.e. required, minItems, maxItems, email, numeric etc.

Supports dynamic validations using conditional, customTask(s).

Supports ValidValues annotation for set of valid values.

Can be used with any framework of Java or a Rest based Java API.

A user validator map, user defined message set, valid value data set can be configured using builder pattern.

Output of the validation contains type and a json path(even the complex one) to rightly identify the issues.

Pass an Object Instance or List instance directly to the validation util.

Steps:

  1. Add Maven dependency
<dependency>
    <groupId>com.github.salilvnair</groupId>
    <artifactId>jsonprocessor</artifactId>
    <version>1.0.5</version>
</dependency>
  1. Annotate a class or a field using @JsonKeyValidator , class should be implementing the marker interface named JsonRequest
@JsonKeyValidator 
public class School implements JsonRequest {
   ....
}
  1. Set it as required, conditional, numeric, email erc.
@JsonKeyValidator(required=true, numeric=true)
private String id;
@JsonKeyValidator(conditional=true, condition="validateName")
private String name;
@JsonKeyValidator(email=true)
private String email;
  1. Call the processor using JsonProcessorBuilder
List<ValidationMessage>  validationMsgList  = new JsonProcessorBuilder()
                                                     .request(school)
                                                     .validate();
  1. User defined map can be passed in the JsonProcessorBuilder which can be later used in customTask(s) or in Conditional validators.
Map<String,Object> validatorMap  = new HashMap<>();
validatorMap.put("alumini", "Hogward");
List<ValidationMessage>  validationMsgList  = new JsonProcessorBuilder()
                                                     .request(school)
                                                     .setUserValidatorMap(validatorMap)
                                                     .validate();

Complete Usage:

@JsonKeyValidator(id="School", customTaskValidator=SchoolCustomTask.class)
public class School implements JsonRequest {
	@JsonKeyValidator(required=true, message="User defined message!")
	private long id;
	@JsonKeyValidator(conditional=true, condition="validateAlumini")
	private String name;
	@JsonKeyValidator(required=true)
	private HeadMaster headMaster;
	@JsonKeyValidator(
		required=true,
		minItems=4,	
		userDefinedMessages = {
				@UserDefinedMessage(
						validatorType=ValidatorType.REQUIRED,
						message="Students are mandatory in a school",
						messageType=MessageType.ERROR
				),
				@UserDefinedMessage(
						validatorType=ValidatorType.MINITEMS,
						message="Minimum 4 students should be there at "+JsonKeyValidatorConstant.PATH_PLACEHOLDER,
						messageType = MessageType.WARNING
				)
		}
	)
	private List<Student> students;
}
public class SchoolCustomTask extends AbstractCustomJsonValidatorTask {
	public String validateAlumini(JsonValidatorContext jsonValidatorContext) {
		School school = (School) jsonValidatorContext.getJsonRequest();
		Map<String,Object> validatorMap = jsonValidatorContext.getUserValidatorMap();
		if(!validatorMap.isEmpty() && school.getName()!=null) {
			String alumini  = (String) validatorMap.get("alumini");
			if(!school.getName().contains(alumini)){
				return "Only "+alumini+" alumini schools are allowed";
			}
		}
		return null;
	}
}

Versions

Version
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0