Class JsObjSpec

java.lang.Object
jsonvalues.spec.JsObjSpec
All Implemented Interfaces:
JsSpec

public final class JsObjSpec extends Object implements JsSpec
Represents a specification of a JSON object, allowing you to define rules and constraints for validating JSON objects. This class is part of the js-values library and is designed to facilitate the validation of JSON data structures. You can create an instance of JsObjSpec to specify the expected structure and constraints of a JSON object.

Usage: - Create an instance of JsObjSpec using the builder methods provided. - Define required and optional keys using withReqKeys and withOptKeys. - Specify conditions that the JSON object must meet using suchThat. - Perform validation using test to check if a given JSON object conforms to the defined specification.

Example:



 JsObjSpec personSpec = JsObjSpec
     .of("age",JsSpecs.integer(),"name",JsSpecs.str())
     .withReqKeys("name", "age")
     .suchThat(person -> person.getInt("age") >= 18);

 // Validation
 JsValue json = // JSON data to validate
 List<SpecError> errors = personSpec.test(JsPath.root(), json);
 
 

The JsObjSpec class provides methods for defining required and optional keys, specifying predicates, and performing validation. Additionally, it supports nullable objects and strict mode to enforce key presence.

See Also: