ObjectSchema extends Schema { public static Builder builder() { return new Builder(); } ObjectSchema(Builder builder); static Builder builder(); Integer getMaxProperties(); Integer getMinProperties(); @Deprecated Map<Pattern, Schema> getPatternProperties(); Map<String, Set<String>> getPropertyDependencies(); Map<String, Schema> getPropertySchemas(); List<String> getRequiredProperties(); Map<String, Schema> getSchemaDependencies(); Schema getSchemaOfAdditionalProperties(); Schema getPropertyNameSchema(); boolean permitsAdditionalProperties(); boolean requiresObject(); @Override boolean definesProperty(String field); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void additionalPropertiesOnEmptyObject() { ObjectSchema.builder() .schemaOfAdditionalProperties(BooleanSchema.INSTANCE).build() .validate(OBJECTS.getJSONObject("emptyObject")); }
@Test public void additionalPropertySchema() { String expectedSchemaLocation = "#/bool/location"; BooleanSchema boolSchema = BooleanSchema.builder().schemaLocation(expectedSchemaLocation).build(); ObjectSchema subject = buildWithLocation(ObjectSchema.builder() .schemaOfAdditionalProperties(boolSchema)); TestSupport.failureOf(subject) .input(OBJECTS.get("additionalPropertySchema")) .expectedPointer("#/foo") .expectedSchemaLocation(expectedSchemaLocation) .expect(); TestSupport.expectFailure(subject, "#/foo", OBJECTS.get("additionalPropertySchema")); }
@Test public void maxPropertiesFailure() { ObjectSchema subject = buildWithLocation(ObjectSchema.builder().maxProperties(2)); TestSupport.failureOf(subject) .input(OBJECTS.get("maxPropertiesFailure")) .expectedPointer("#") .expectedKeyword("maxProperties") .expect(); }
@Test public void minPropertiesFailure() { ObjectSchema subject = buildWithLocation(ObjectSchema.builder().minProperties(2)); TestSupport.failureOf(subject) .input(OBJECTS.get("minPropertiesFailure")) .expectedPointer("#") .expectedKeyword("minProperties") .expect(); }
@Test public void multipleAdditionalProperties() { ObjectSchema subject = buildWithLocation(ObjectSchema.builder().additionalProperties(false)); try { subject.validate(new JSONObject("{\"a\":true,\"b\":true}")); fail("did not throw exception for multiple additional properties"); } catch (ValidationException e) { assertEquals("#: 2 schema violations found", e.getMessage()); assertEquals(2, e.getCausingExceptions().size()); } }
@Test public void multipleSchemaDepViolation() { Schema billingAddressSchema = new StringSchema(); Schema billingNameSchema = StringSchema.builder().minLength(4).build(); ObjectSchema subject = ObjectSchema.builder() .addPropertySchema("name", new StringSchema()) .addPropertySchema("credit_card", NumberSchema.builder().build()) .schemaDependency("credit_card", ObjectSchema.builder() .addPropertySchema("billing_address", billingAddressSchema) .addRequiredProperty("billing_address") .addPropertySchema("billing_name", billingNameSchema) .build()) .schemaDependency("name", ObjectSchema.builder() .addRequiredProperty("age") .build()) .build(); try { subject.validate(OBJECTS.get("schemaDepViolation")); fail("did not throw ValidationException"); } catch (ValidationException e) { ValidationException creditCardFailure = e.getCausingExceptions().get(0); ValidationException ageFailure = e.getCausingExceptions().get(1); if (creditCardFailure.getCausingExceptions().isEmpty()) { ValidationException tmp = creditCardFailure; creditCardFailure = ageFailure; ageFailure = tmp; } ValidationException billingAddressFailure = creditCardFailure.getCausingExceptions().get(0); assertEquals("#/billing_address", billingAddressFailure.getPointerToViolation()); assertEquals(billingAddressSchema, billingAddressFailure.getViolatedSchema()); ValidationException billingNameFailure = creditCardFailure .getCausingExceptions().get(1); assertEquals("#/billing_name", billingNameFailure.getPointerToViolation()); assertEquals(billingNameSchema, billingNameFailure.getViolatedSchema()); assertEquals("#", ageFailure.getPointerToViolation()); assertEquals("#: required key [age] not found", ageFailure.getMessage()); } }
@Test public void noAdditionalProperties() { ObjectSchema subject = ObjectSchema.builder().additionalProperties(false).build(); TestSupport.expectFailure(subject, "#", OBJECTS.get("propertySchemaViolation")); }
@Test public void noProperties() { ObjectSchema.builder().build().validate(OBJECTS.get("noProperties")); }
@Test public void patternPropertyOnEmptyObjct() { ObjectSchema.builder() .patternProperty("b_.*", BooleanSchema.INSTANCE) .build().validate(new JSONObject()); }
@Test public void patternPropertyOverridesAdditionalPropSchema() { ObjectSchema.builder() .schemaOfAdditionalProperties(new NumberSchema()) .patternProperty("aa.*", BooleanSchema.INSTANCE) .build().validate(OBJECTS.get("patternPropertyOverridesAdditionalPropSchema")); }
@Test public void patternPropertyViolation() { ObjectSchema subject = ObjectSchema.builder() .patternProperty("^b_.*", BooleanSchema.INSTANCE) .patternProperty("^s_.*", new StringSchema()) .build(); TestSupport.expectFailure(subject, BooleanSchema.INSTANCE, "#/b_1", OBJECTS.get("patternPropertyViolation")); }
@Test public void patternPropsOverrideAdditionalProps() { ObjectSchema.builder() .patternProperty("^v.*", EmptySchema.INSTANCE) .additionalProperties(false) .build().validate(OBJECTS.get("patternPropsOverrideAdditionalProps")); }
@Test public void propertyDepViolation() { ObjectSchema subject = buildWithLocation( ObjectSchema.builder() .addPropertySchema("ifPresent", NullSchema.INSTANCE) .addPropertySchema("mustBePresent", BooleanSchema.INSTANCE) .propertyDependency("ifPresent", "mustBePresent") ); TestSupport.failureOf(subject) .input(OBJECTS.get("propertyDepViolation")) .expectedKeyword("dependencies") .expect(); }
@Test public void propertySchemaViolation() { ObjectSchema subject = ObjectSchema.builder() .addPropertySchema("boolProp", BooleanSchema.INSTANCE).build(); TestSupport.expectFailure(subject, BooleanSchema.INSTANCE, "#/boolProp", OBJECTS.get("propertySchemaViolation")); }
@Test public void requiredProperties() { ObjectSchema subject = buildWithLocation( ObjectSchema.builder() .addPropertySchema("boolProp", BooleanSchema.INSTANCE) .addPropertySchema("nullProp", NullSchema.INSTANCE) .addRequiredProperty("boolProp") ); TestSupport.failureOf(subject) .expectedPointer("#") .expectedKeyword("required") .input(OBJECTS.get("requiredProperties")) .expect(); }
@Test public void requireObject() { TestSupport.expectFailure(buildWithLocation(ObjectSchema.builder()), "#", "foo"); }
@Test public void schemaDepViolation() { Schema billingAddressSchema = new StringSchema(); ObjectSchema subject = ObjectSchema.builder() .addPropertySchema("name", new StringSchema()) .addPropertySchema("credit_card", NumberSchema.builder().build()) .schemaDependency("credit_card", ObjectSchema.builder() .addPropertySchema("billing_address", billingAddressSchema) .addRequiredProperty("billing_address") .build()) .build(); TestSupport.expectFailure(subject, billingAddressSchema, "#/billing_address", OBJECTS.get("schemaDepViolation")); }
@Test(expected = SchemaException.class) public void schemaForNoAdditionalProperties() { ObjectSchema.builder().additionalProperties(false) .schemaOfAdditionalProperties(BooleanSchema.INSTANCE).build(); }
@Test public void typeFailure() { TestSupport.failureOf(ObjectSchema.builder()) .expectedKeyword("type") .input("a") .expect(); }
@Test public void toStringWithDefault() { JSONObject rawSchemaJson = loader.readObj("tostring/objectschema-default.json"); Schema schema = ObjectSchema.builder() .addPropertySchema("a", EmptySchema.builder() .defaultValue(new JSONObject("{\"description\":\"default empty value\"}")) .build()) .build(); String actual = schema.toString(); assertThat(new JSONObject(actual), sameJsonAs(rawSchemaJson)); }
@Test public void propertyNamesFailure() { StringSchema propNameSchema = StringSchema.builder() .minLength(5) .maxLength(7) .schemaLocation("#/propertyNames") .build(); ObjectSchema.Builder subject = ObjectSchema.builder() .propertyNameSchema(propNameSchema ); TestSupport.failureOf(subject) .expectedViolatedSchema(propNameSchema) .expectedPointer("#/a") .expectedSchemaLocation("#/propertyNames") .input(new JSONObject("{\"a\":null}")) .expect(); }
@Test public void emptyObjectPropertyNamesSchema() { Schema subject = ObjectSchema.builder().propertyNameSchema(StringSchema.builder().build()).build(); subject.validate(new JSONObject("{}")); }