ObjectSchemaLoader { ObjectSchema.Builder load() { ObjectSchema.Builder builder = ObjectSchema.builder(); ls.schemaJson().maybe("minProperties").map(JsonValue::requireInteger).ifPresent(builder::minProperties); ls.schemaJson().maybe("maxProperties").map(JsonValue::requireInteger).ifPresent(builder::maxProperties); ls.schemaJson().maybe("properties").map(JsonValue::requireObject) .ifPresent(propertyDefs -> populatePropertySchemas(propertyDefs, builder)); ls.schemaJson().maybe("additionalProperties").ifPresent(rawAddProps -> { rawAddProps.canBe(Boolean.class, p -> builder.additionalProperties(p)) .or(JsonObject.class, def -> builder.schemaOfAdditionalProperties(defaultLoader.loadChild(def).build())) .requireAny(); }); ls.schemaJson().maybe("required").map(JsonValue::requireArray) .ifPresent(arr -> arr.forEach((i, val) -> builder.addRequiredProperty(val.requireString()))); ls.schemaJson().maybe("patternProperties").map(JsonValue::requireObject) .ifPresent(patternProps -> { patternProps.keySet().forEach(pattern -> { Schema patternSchema = defaultLoader.loadChild(patternProps.require(pattern)).build(); Regexp regexp = ls.config.regexpFactory.createHandler(pattern); builder.patternProperty(regexp, patternSchema); }); }); ls.schemaJson().maybe("dependencies").map(JsonValue::requireObject) .ifPresent(deps -> addDependencies(builder, deps)); if (ls.specVersion().isAtLeast(DRAFT_6)) { ls.schemaJson().maybe("propertyNames") .map(defaultLoader::loadChild) .map(Schema.Builder::build) .ifPresent(builder::propertyNameSchema); } return builder; } ObjectSchemaLoader(LoadingState ls, LoaderConfig config, SchemaLoader defaultLoader);   }
@Test public void objectSchema() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectSchema")); assertNotNull(actual); Map<String, Schema> propertySchemas = actual.getPropertySchemas(); assertEquals(2, propertySchemas.size()); assertEquals(BooleanSchema.INSTANCE, propertySchemas.get("boolProp")); Assert.assertFalse(actual.permitsAdditionalProperties()); assertEquals(2, actual.getRequiredProperties().size()); assertEquals(2, actual.getMinProperties().intValue()); assertEquals(3, actual.getMaxProperties().intValue()); }
@Test(expected = SchemaException.class) public void objectInvalidAdditionalProperties() { SchemaLoader.load(get("objectInvalidAdditionalProperties")); }
@Test public void objectWithAdditionalPropSchema() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithAdditionalPropSchema")); assertEquals(BooleanSchema.INSTANCE, actual.getSchemaOfAdditionalProperties()); }
@Test public void objectWithPropDep() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithPropDep")); assertEquals(1, actual.getPropertyDependencies().get("isIndividual").size()); }
@Test public void objectWithSchemaDep() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithSchemaDep")); assertEquals(1, actual.getSchemaDependencies().size()); }
@Test public void patternProperties() { ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternProperties")); assertNotNull(actual); assertEquals(2, actual.getPatternProperties().size()); }
@Test(expected = SchemaException.class) public void invalidDependency() { SchemaLoader.load(get("invalidDependency")); }
@Test public void emptyDependencyList() { SchemaLoader.load(get("emptyDependencyList")); }
@Test public void invalidRequired() { expExc.expect(SchemaException.class); expExc.expectMessage("#/required/1: expected type: String, found: JsonArray"); SchemaLoader.load(get("invalidRequired")); }
@Test public void properyNamesV4() { JSONObject rawSchema = get("propertyNames"); rawSchema.put("type", "object"); ObjectSchema actual = (ObjectSchema) SchemaLoader.load(rawSchema); assertNull(actual.getPropertyNameSchema()); }