Java Code Examples for org.everit.json.schema.ObjectSchema#Builder

The following examples show how to use org.everit.json.schema.ObjectSchema#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSchemaFromFieldDescriptorsGenerator.java    From restdocs-raml with MIT License 6 votes vote down vote up
private Schema traverse(List<String> traversedSegments, List<JsonFieldPath> jsonFieldPaths, ObjectSchema.Builder builder) {

        Map<String, List<JsonFieldPath>> groupedFields = groupFieldsByFirstRemainingPathSegment(traversedSegments, jsonFieldPaths);
        groupedFields.forEach((propertyName, fieldList) -> {

            List<String> newTraversedSegments = new ArrayList<>(traversedSegments);
            newTraversedSegments.add(propertyName);
            fieldList.stream()
                    .filter(isDirectMatch(newTraversedSegments))
                    .findFirst()
                    .map(directMatch -> {
                        if (fieldList.size() == 1) {
                            handleEndOfPath(builder, propertyName, directMatch.getFieldDescriptor());
                        } else {
                            List<JsonFieldPath> newFields = new ArrayList<>(fieldList);
                            newFields.remove(directMatch);
                            processRemainingSegments(builder, propertyName, newTraversedSegments, newFields, (String) directMatch.getFieldDescriptor().getDescription());
                        }
                        return true;
                    }).orElseGet(() -> {
                        processRemainingSegments(builder, propertyName, newTraversedSegments, fieldList, null);
                        return true;
                    });
        });
        return builder.build();
    }
 
Example 2
Source File: JsonSchemaFromFieldDescriptorsGenerator.java    From restdocs-raml with MIT License 5 votes vote down vote up
private void processRemainingSegments(ObjectSchema.Builder builder, String propertyName, List<String> traversedSegments, List<JsonFieldPath> fields, String description) {
    List<String> remainingSegments = fields.get(0).remainingSegments(traversedSegments);
    if (remainingSegments.size() > 0 && isArraySegment(remainingSegments.get(0))) {
        traversedSegments.add(remainingSegments.get(0));
        builder.addPropertySchema(propertyName, ArraySchema.builder()
                .allItemSchema(traverse(traversedSegments, fields, ObjectSchema.builder()))
                .description(description)
                .build());
    } else {
        builder.addPropertySchema(propertyName, traverse(traversedSegments, fields, (ObjectSchema.Builder) ObjectSchema.builder()
                .description(description)));
    }
}
 
Example 3
Source File: JsonSchemaFromFieldDescriptorsGenerator.java    From restdocs-raml with MIT License 5 votes vote down vote up
private void handleEndOfPath(ObjectSchema.Builder builder, String propertyName, FieldDescriptor fieldDescriptor) {

        if (fieldDescriptor.isIgnored()) {
            // We don't need to render anything
        } else {
            if (isRequired(fieldDescriptor)) {
                builder.addRequiredProperty(propertyName);
            }
            if (fieldDescriptor.getType().equals(JsonFieldType.NULL) || fieldDescriptor.getType().equals(JsonFieldType.VARIES)) {
                builder.addPropertySchema(propertyName, NullSchema.builder()
                        .description((String) fieldDescriptor.getDescription())
                        .build());
            } else if (fieldDescriptor.getType().equals(JsonFieldType.OBJECT)) {
                builder.addPropertySchema(propertyName, ObjectSchema.builder()
                        .description((String) fieldDescriptor.getDescription())
                        .build());

            } else if (fieldDescriptor.getType().equals(JsonFieldType.ARRAY)) {
                builder.addPropertySchema(propertyName, ArraySchema.builder()
                        .description((String) fieldDescriptor.getDescription())
                        .build());
            } else if (fieldDescriptor.getType().equals(JsonFieldType.BOOLEAN)) {
                builder.addPropertySchema(propertyName, BooleanSchema.builder()
                        .description((String) fieldDescriptor.getDescription())
                        .build());
            } else if (fieldDescriptor.getType().equals(JsonFieldType.NUMBER)) {
                builder.addPropertySchema(propertyName, NumberSchema.builder()
                        .description((String) fieldDescriptor.getDescription())
                        .build());
            } else if (fieldDescriptor.getType().equals(JsonFieldType.STRING)) {
                builder.addPropertySchema(propertyName, StringSchema.builder()
                        .minLength(minLengthString(fieldDescriptor))
                        .maxLength(maxLengthString(fieldDescriptor))
                        .description((String) fieldDescriptor.getDescription())
                        .build());
            } else {
                throw new IllegalArgumentException("unknown field type " + fieldDescriptor.getType());
            }
        }
    }
 
Example 4
Source File: ObjectSchemaLoader.java    From json-schema with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: ObjectSchemaLoader.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private void populatePropertySchemas(JsonObject propertyDefs,
        ObjectSchema.Builder builder) {
    propertyDefs.forEach((key, value) -> {
        if (!key.equals(ls.specVersion().idKeyword())
                || value instanceof JsonObject) {
            addPropertySchemaDefinition(key, value, builder);
        }
    });
}
 
Example 6
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 4 votes vote down vote up
ObjectSchema.Builder buildObjectSchema() {
    config().specVersion.objectKeywords().forEach(consumedKeys::keyConsumed);
    return new ObjectSchemaLoader(schemaJson.ls, config(), defaultLoader).load();
}
 
Example 7
Source File: ObjectSchemaLoader.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private void addPropertySchemaDefinition(String keyOfObj, JsonValue definition, ObjectSchema.Builder builder) {
    builder.addPropertySchema(keyOfObj, defaultLoader.loadChild(definition).build());
}
 
Example 8
Source File: ObjectSchemaLoader.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private void addDependencies(ObjectSchema.Builder builder, JsonObject deps) {
    deps.forEach((ifPresent, mustBePresent) -> addDependency(builder, ifPresent, mustBePresent));
}
 
Example 9
Source File: ObjectSchemaLoader.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private void addDependency(ObjectSchema.Builder builder, String ifPresent, JsonValue deps) {
    deps.canBeSchema(obj -> builder.schemaDependency(ifPresent, defaultLoader.loadChild(obj).build()))
            .or(JsonArray.class, arr -> arr.forEach((i, entry) -> builder.propertyDependency(ifPresent, entry.requireString())))
            .requireAny();
}