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

The following examples show how to use org.everit.json.schema.Schema#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: SchemaExtractor.java    From json-schema with Apache License 2.0 6 votes vote down vote up
private Schema.Builder<?> loadForExplicitType(String typeString) {
    switch (typeString) {
    case "string":
        return buildStringSchema().requiresString(true);
    case "integer":
        return buildNumberSchema().requiresInteger(true);
    case "number":
        return buildNumberSchema();
    case "boolean":
        return BooleanSchema.builder();
    case "null":
        return NullSchema.builder();
    case "array":
        return buildArraySchema();
    case "object":
        return buildObjectSchema();
    default:
        throw new SchemaException(schemaJson.ls.locationOfCurrentObj(), format("unknown type: [%s]", typeString));
    }
}
 
Example 2
Source File: SchemaLoader.java    From json-schema with Apache License 2.0 6 votes vote down vote up
private AdjacentSchemaExtractionState loadCommonSchemaProperties(Schema.Builder builder, AdjacentSchemaExtractionState state) {
    KeyConsumer consumedKeys = new KeyConsumer(state.projectedSchemaJson());
    consumedKeys.maybe(config.specVersion.idKeyword()).map(JsonValue::requireString).ifPresent(builder::id);
    consumedKeys.maybe("title").map(JsonValue::requireString).ifPresent(builder::title);
    consumedKeys.maybe("description").map(JsonValue::requireString).ifPresent(builder::description);
    if (ls.specVersion() == DRAFT_7) {
        consumedKeys.maybe("readOnly").map(JsonValue::requireBoolean).ifPresent(builder::readOnly);
        consumedKeys.maybe("writeOnly").map(JsonValue::requireBoolean).ifPresent(builder::writeOnly);
    }
    if (config.nullableSupport) {
        builder.nullable(consumedKeys.maybe("nullable")
                .map(JsonValue::requireBoolean)
                .orElse(Boolean.FALSE));
    }
    if (config.useDefaults) {
        consumedKeys.maybe("default").map(JsonValue::deepToOrgJson).ifPresent(builder::defaultValue);
    }
    builder.schemaLocation(ls.pointerToCurrentObj);
    return state.reduce(new ExtractionResult(consumedKeys.collect(), emptyList()));
}
 
Example 3
Source File: SchemaLoader.java    From json-schema with Apache License 2.0 6 votes vote down vote up
private Schema.Builder loadSchemaObject(JsonObject o) {
    AdjacentSchemaExtractionState postExtractionState = runSchemaExtractors(o);
    Collection<Schema.Builder<?>> extractedSchemas = postExtractionState.extractedSchemaBuilders();
    Schema.Builder effectiveReturnedSchema;
    if (extractedSchemas.isEmpty()) {
        effectiveReturnedSchema = EmptySchema.builder();
    } else if (extractedSchemas.size() == 1) {
        effectiveReturnedSchema = extractedSchemas.iterator().next();
    } else {
        Collection<Schema> built = extractedSchemas.stream()
                .map(Schema.Builder::build)
                .map(Schema.class::cast)
                .collect(toList());
        effectiveReturnedSchema = CombinedSchema.allOf(built).isSynthetic(true);
    }
    AdjacentSchemaExtractionState postCommonPropLoadingState = loadCommonSchemaProperties(effectiveReturnedSchema, postExtractionState);
    Map<String, Object> unprocessed = postCommonPropLoadingState.projectedSchemaJson().toMap();
    effectiveReturnedSchema.unprocessedProperties(unprocessed);
    return effectiveReturnedSchema;
}
 
Example 4
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 6 votes vote down vote up
@Override List<Schema.Builder<?>> extract() {
    List<Schema.Builder<?>> builders = new ArrayList<>(1);
    if (schemaHasAnyOf(config().specVersion.arrayKeywords())) {
        builders.add(buildArraySchema().requiresArray(false));
    }
    if (schemaHasAnyOf(config().specVersion.objectKeywords())) {
        builders.add(buildObjectSchema().requiresObject(false));
    }
    if (schemaHasAnyOf(NUMBER_SCHEMA_PROPS)) {
        builders.add(buildNumberSchema().requiresNumber(false));
    }
    if (schemaHasAnyOf(STRING_SCHEMA_PROPS)) {
        builders.add(buildStringSchema().requiresString(false));
    }
    if (config().specVersion.isAtLeast(DRAFT_7) && schemaHasAnyOf(CONDITIONAL_SCHEMA_KEYWORDS)) {
        builders.add(buildConditionalSchema());
    }
    return builders;
}
 
Example 5
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Override List<Schema.Builder<?>> extract() {
    if (!containsKey("enum")) {
        return emptyList();
    }
    EnumSchema.Builder builder = EnumSchema.builder();
    List<Object> possibleValues = new ArrayList<>();
    require("enum").requireArray().forEach((i, item) -> possibleValues.add(item.unwrap()));
    builder.possibleValues(possibleValues);
    return singletonList(builder);
}
 
Example 6
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Override List<Schema.Builder<?>> extract() {
    if (containsKey("type")) {
        return singletonList(require("type").canBeMappedTo(JsonArray.class, arr -> (Schema.Builder) buildAnyOfSchemaForMultipleTypes())
                .orMappedTo(String.class, this::loadForExplicitType)
                .requireAny());
    } else {
        return emptyList();
    }
}
 
Example 7
Source File: ReferenceLookupTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private ReferenceSchema obtainReferenceSchema(String pointerToRef) {
    JsonObject jsonValue = query(pointerToRef).requireObject();
    ReferenceLookup subject = new ReferenceLookup(jsonValue.ls);
    String refPointer = jsonValue.require("$ref").requireString();
    Schema.Builder<?> actual = subject.lookup(refPointer, jsonValue);
    return (ReferenceSchema) actual.build();
}
 
Example 8
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Override List<Schema.Builder<?>> extract() {
    if (config().specVersion != DRAFT_4 && containsKey("const")) {
        return singletonList(ConstSchema.builder().permittedValue(require("const").unwrap()));
    } else {
        return emptyList();
    }
}
 
Example 9
Source File: CombinedSchemaLoader.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@Override
public ExtractionResult extract(JsonObject schemaJson) {
    Set<String> presentKeys = COMB_SCHEMA_PROVIDERS.keySet().stream()
            .filter(schemaJson::containsKey)
            .collect(toSet());
    Collection<Schema.Builder<?>> extractedSchemas = presentKeys.stream().map(key -> loadCombinedSchemaForKeyword(schemaJson, key))
            .collect(toList());
    return new ExtractionResult(presentKeys, extractedSchemas);
}
 
Example 10
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private Schema.Builder<?> performQueryEvaluation(String mapKey, JsonPointerEvaluator pointerEvaluator) {
    String absolutePointer = ReferenceResolver.resolve(ls.id, mapKey).toString();
    if (ls.pointerSchemas.containsKey(absolutePointer)) {
        return ls.pointerSchemas.get(absolutePointer).initReference(absolutePointer);
    }
    JsonValue referencedRawSchema = pointerEvaluator.query().getQueryResult();
    return createReferenceSchema(mapKey, absolutePointer, referencedRawSchema);
}
 
Example 11
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a schema builder instance after looking up the JSON pointer.
 */
Schema.Builder<?> lookup(String relPointerString, JsonObject ctx) {
    String absPointerString = ReferenceResolver.resolve(ls.id, relPointerString).toString();
    if (ls.pointerSchemas.containsKey(absPointerString)) {
        return ls.pointerSchemas.get(absPointerString).initReference(absPointerString);
    }
    JsonValue rawInternalReferenced = lookupObjById(ls.rootSchemaJson, absPointerString);
    if (rawInternalReferenced != null) {
        return createReferenceSchema(relPointerString, absPointerString, rawInternalReferenced);
    }
    if (isSameDocumentRef(relPointerString)) {
        return performQueryEvaluation(relPointerString, JsonPointerEvaluator.forDocument(ls.rootSchemaJson(), relPointerString));
    }
    JsonPointerEvaluator pointer = createPointerEvaluator(absPointerString);
    ReferenceKnot knot = new ReferenceKnot();
    ReferenceSchema.Builder refBuilder = knot.initReference(relPointerString);
    ls.pointerSchemas.put(absPointerString, knot);
    JsonPointerEvaluator.QueryResult result = pointer.query();

    URI resolutionScope = !isSameDocumentRef(absPointerString) ? withoutFragment(absPointerString) : ls.id;
    JsonObject containingDocument = result.getContainingDocument();
    SchemaLocation resultLocation = result.getQueryResult().ls.pointerToCurrentObj;
    SchemaLoader childLoader = ls.initNewDocumentLoader()
            .resolutionScope(resolutionScope)
            .pointerToCurrentObj(resultLocation)
            .schemaJson(result.getQueryResult())
            .rootSchemaJson(containingDocument).build();
    Schema referredSchema = childLoader.load().build();
    refBuilder.schemaLocation(resultLocation);
    knot.resolveWith(referredSchema);
    return refBuilder;
}
 
Example 12
Source File: ReferenceLookup.java    From json-schema with Apache License 2.0 5 votes vote down vote up
private Schema.Builder<?> createReferenceSchema(String relPointerString, String absPointerString, JsonValue rawReferenced) {
    ReferenceKnot knot = new ReferenceKnot();
    ReferenceSchema.Builder refBuilder = knot.initReference(relPointerString);
    ls.pointerSchemas.put(absPointerString, knot);
    Schema referredSchema = new SchemaLoader(rawReferenced.ls).load().build();
    knot.resolveWith(referredSchema);
    return refBuilder;
}
 
Example 13
Source File: AdjacentSchemaExtractionState.java    From json-schema with Apache License 2.0 5 votes vote down vote up
AdjacentSchemaExtractionState reduce(ExtractionResult result) {
    Set<Schema.Builder<?>> newExtractedSchemas = new HashSet<>(extractedSchemas.size() + result.extractedSchemas.size());
    newExtractedSchemas.addAll(extractedSchemas);
    newExtractedSchemas.addAll(result.extractedSchemas);
    JsonObject projectedContext = new ProjectedJsonObject(context, result.consumedKeys);
    return new AdjacentSchemaExtractionState(projectedContext, newExtractedSchemas);
}
 
Example 14
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 4 votes vote down vote up
ExtractionResult(String consumedKeys, Collection<Schema.Builder<?>> extactedSchemas) {
    this(singleton(consumedKeys), extactedSchemas);
}
 
Example 15
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 4 votes vote down vote up
ExtractionResult(Set<String> consumedKeys, Collection<Schema.Builder<?>> extractedSchemas) {
    this.consumedKeys = requireNonNull(consumedKeys, "consumedKeys cannot be null");
    this.extractedSchemas = requireNonNull(extractedSchemas, "extractedSchemas cannot be null");
}
 
Example 16
Source File: AdjacentSchemaExtractionState.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private AdjacentSchemaExtractionState(JsonObject context, Set<Schema.Builder<?>> extractedSchemas) {
    this.context = context;
    this.extractedSchemas = extractedSchemas;
}
 
Example 17
Source File: SchemaLoader.java    From json-schema with Apache License 2.0 4 votes vote down vote up
private Schema.Builder loadSchemaBoolean(Boolean rawBoolean) {
    return rawBoolean ? TrueSchema.builder() : FalseSchema.builder();
}
 
Example 18
Source File: AdjacentSchemaExtractionState.java    From json-schema with Apache License 2.0 4 votes vote down vote up
public Collection<Schema.Builder<?>> extractedSchemaBuilders() {
    return extractedSchemas;
}
 
Example 19
Source File: SchemaLoader.java    From json-schema with Apache License 2.0 3 votes vote down vote up
/**
 * Populates a {@code Schema.Builder} instance from the {@code schemaJson} schema definition.
 *
 * @return the builder which already contains the validation criteria of the schema, therefore
 * {@link Schema.Builder#build()} can be immediately used to acquire the {@link Schema}
 * instance to be used for validation
 */
public Schema.Builder<?> load() {
    return ls.schemaJson
            .canBeMappedTo(Boolean.class, this::loadSchemaBoolean)
            .orMappedTo(JsonObject.class, this::loadSchemaObject)
            .requireAny();
}
 
Example 20
Source File: SchemaExtractor.java    From json-schema with Apache License 2.0 votes vote down vote up
abstract List<Schema.Builder<?>> extract();