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

The following examples show how to use org.everit.json.schema.Schema. These examples are extracted from open source projects. 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 Project: apicurio-registry   Author: Apicurio   File: JsonSchemaDiffLibrary.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Find and analyze differences between two JSON schemas.
 *
 * @param original Original/Previous/First/Left JSON schema representation
 * @param updated  Updated/Next/Second/Right JSON schema representation
 * @return an object to access the found differences: Original -> Updated
 * @throws IllegalArgumentException if the input is not a valid representation of a JsonSchema
 */
public static DiffContext findDifferences(String original, String updated) {
    try {
        JSONObject originalJson = MAPPER.readValue(original, JSONObject.class);
        JSONObject updatedJson = MAPPER.readValue(updated, JSONObject.class);

        Schema originalSchema = SchemaLoader.builder()
            .schemaJson(originalJson)
            .build().load().build();

        Schema updatedSchema = SchemaLoader.builder()
            .schemaJson(updatedJson)
            .build().load().build();

        return findDifferences(originalSchema, updatedSchema);

    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}
 
Example #2
Source Project: AppiumTestDistribution   Author: AppiumTestDistribution   File: CapabilitySchemaValidator.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void validateCapabilitySchema(JSONObject capability) {
    try {
        isPlatformInEnv();
        InputStream inputStream = getClass().getResourceAsStream(getPlatform());
        JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
        Schema schema = SchemaLoader.load(rawSchema);
        schema.validate(new JSONObject(capability.toString()));
        validateRemoteHosts();
    } catch (ValidationException e) {
        if (e.getCausingExceptions().size() > 1) {
            e.getCausingExceptions().stream()
                .map(ValidationException::getMessage)
                .forEach(System.out::println);
        } else {
            LOGGER.info(e.getErrorMessage());
        }

        throw new ValidationException("Capability json provided is missing the above schema");
    }
}
 
Example #3
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: 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 #4
Source Project: apicurio-registry   Author: Apicurio   File: SchemaDiffVisitor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void visitConstSchema(ConstSchemaWrapper schema) {
    Schema orig = original;

    if (orig instanceof FalseSchema)
        return; // FalseSchema matches nothing

    // Const and single-enum equivalency
    if (orig instanceof EnumSchema) {
        Set<Object> possibleValues = ((EnumSchema) orig).getPossibleValues();
        if (possibleValues.size() == 1) {
            orig = ConstSchema.builder()
                .permittedValue(possibleValues.stream().findAny().get())
                .build();
        }
    }

    if (!(orig instanceof ConstSchema)) {
        ctx.addDifference(SUBSCHEMA_TYPE_CHANGED, orig, schema);
        return;
    }
    schema.accept(new ConstSchemaDiffVisitor(ctx, (ConstSchema) orig));
}
 
Example #5
Source Project: apicurio-registry   Author: Apicurio   File: SchemaDiffVisitor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void visitEnumSchema(EnumSchemaWrapper schema) {
    Schema orig = original;

    if (orig instanceof FalseSchema)
        return; // FalseSchema matches nothing

    // Const and single-enum equivalency
    if (orig instanceof ConstSchema) {
        Object permittedValue = ((ConstSchema) orig).getPermittedValue();
        orig = EnumSchema.builder()
            .possibleValue(permittedValue)
            .build();
    }

    if (!(orig instanceof EnumSchema)) {
        ctx.addDifference(SUBSCHEMA_TYPE_CHANGED, orig, schema);
        return;
    }
    schema.accept(new EnumSchemaDiffVisitor(ctx, (EnumSchema) orig));
}
 
Example #6
Source Project: molgenis   Author: molgenis   File: JsonValidatorTest.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testLoadAndValidate() {
  String schemaJson =
      gson.toJson(
          of(
              "title",
              "Hello World Job",
              "type",
              "object",
              "properties",
              of("delay", of("type", "integer")),
              "required",
              singletonList("delay")));
  Schema schema = jsonValidator.loadSchema(schemaJson);
  jsonValidator.validate("{\"delay\":10}", schema);
}
 
Example #7
Source Project: restdocs-raml   Author: ePages-de   File: JsonSchemaFromFieldDescriptorsGenerator.java    License: 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 #8
Source Project: nakadi   Author: zalando   File: EventValidatorBuilder.java    License: MIT License 6 votes vote down vote up
public EventTypeValidator build(final EventType eventType) {
    final List<Function<JSONObject, Optional<ValidationError>>> validators = new ArrayList<>(2);

    // 1. We always validate schema.
    final Schema schema = SchemaLoader.builder()
            .schemaJson(loader.effectiveSchema(eventType))
            .addFormatValidator(new RFC3339DateTimeValidator())
            .build()
            .load()
            .build();
    validators.add((evt) -> validateSchemaConformance(schema, evt));

    // 2. in case of data or business event type we validate occurred_at
    if (eventType.getCategory() == EventCategory.DATA || eventType.getCategory() == EventCategory.BUSINESS) {
        validators.add(this::validateOccurredAt);
    }

    return (event) -> validators
            .stream()
            .map(validator -> validator.apply(event))
            .filter(Optional::isPresent)
            .findFirst()
            .orElse(Optional.empty());
}
 
Example #9
Source Project: nakadi   Author: zalando   File: ArraySchemaDiff.java    License: MIT License 6 votes vote down vote up
private static void compareItemSchemaArray(
        final ArraySchema original, final ArraySchema update, final SchemaDiffState state) {
    final List<Schema> emptyList = ImmutableList.of();
    final List<Schema> originalSchemas = MoreObjects.firstNonNull(original.getItemSchemas(), emptyList);
    final List<Schema> updateSchemas = MoreObjects.firstNonNull(update.getItemSchemas(), emptyList);

    if (originalSchemas.size() != updateSchemas.size()) {
        state.addChange(NUMBER_OF_ITEMS_CHANGED);
    } else {
        final Iterator<Schema> originalIterator = originalSchemas.iterator();
        final Iterator<Schema> updateIterator = updateSchemas.iterator();
        int index = 0;
        while (originalIterator.hasNext()) {
            state.runOnPath("items/" + index, () -> {
                SchemaDiff.recursiveCheck(originalIterator.next(), updateIterator.next(), state);
            });
            index += 1;
        }
    }
}
 
Example #10
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: 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 #11
Source Project: molgenis   Author: molgenis   File: JsonValidatorTest.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
void testLoadAndValidateInvalid() {
  String schemaJson =
      gson.toJson(
          of(
              "title",
              "Hello World Job",
              "type",
              "object",
              "properties",
              of("p1", of("type", "integer"), "p2", of("type", "integer")),
              "required",
              singletonList("p2")));
  Schema schema = jsonValidator.loadSchema(schemaJson);
  try {
    jsonValidator.validate("{\"p1\":\"10\"}", schema);
  } catch (JsonValidationException expected) {
    assertEquals(
        newHashSet(
            new ConstraintViolation("#/p1: expected type: Number, found: String"),
            new ConstraintViolation("#: required key [p2] not found")),
        expected.getViolations());
  }
}
 
Example #12
Source Project: json-schema   Author: everit-org   File: SchemaLoader.java    License: 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 #13
Source Project: nakadi   Author: zalando   File: SchemaDiffTest.java    License: MIT License 6 votes vote down vote up
@Test
public void checkJsonSchemaCompatibility() throws Exception {
    final JSONArray testCases = new JSONArray(
            readFile("invalid-schema-evolution-examples.json"));

    for (final Object testCaseObject : testCases) {
        final JSONObject testCase = (JSONObject) testCaseObject;
        final Schema original = SchemaLoader.load(testCase.getJSONObject("original_schema"));
        final Schema update = SchemaLoader.load(testCase.getJSONObject("update_schema"));
        final List<String> errorMessages = testCase
                .getJSONArray("errors")
                .toList()
                .stream()
                .map(Object::toString)
                .collect(toList());
        final String description = testCase.getString("description");

        assertThat(description, service.collectChanges(original, update).stream()
                .map(change -> change.getType().toString() + " " + change.getJsonPath())
                .collect(toList()), is(errorMessages));

    }
}
 
Example #14
Source Project: json-schema   Author: everit-org   File: SchemaLoader.java    License: 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 #15
Source Project: apicurio-registry   Author: Apicurio   File: ReferenceSchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
public ReferenceSchemaDiffVisitor(DiffContext ctx, Schema original) {
    this.ctx = ctx;
    if (original instanceof ReferenceSchema) {
        this.referredOriginal = ((ReferenceSchema) original).getReferredSchema();
    } else {
        this.referredOriginal = original;
    }
}
 
Example #16
Source Project: apicurio-registry   Author: Apicurio   File: DiffUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void compareSchema(DiffContext ctx, Schema original, Schema updated,
                                 DiffType addedType, DiffType removedType,
                                 DiffType bothType,
                                 DiffType backwardNotForwardType,
                                 DiffType forwardNotBackwardType,
                                 DiffType none) {
    if (diffAddedRemoved(ctx, original, updated, addedType, removedType)) {
        DiffContext rootCtx = DiffContext.createRootContext();
        new SchemaDiffVisitor(rootCtx, original)
            .visit(wrap(updated));
        boolean backward = rootCtx.foundAllDifferencesAreCompatible();

        rootCtx = DiffContext.createRootContext();
        new SchemaDiffVisitor(rootCtx, updated)
            .visit(wrap(original));
        boolean forward = rootCtx.foundAllDifferencesAreCompatible();

        if (backward && forward) {
            ctx.addDifference(bothType, original, updated);
        }
        if (backward && !forward) {
            ctx.addDifference(backwardNotForwardType, original, updated);
        }
        if (!backward && forward) {
            ctx.addDifference(forwardNotBackwardType, original, updated);
        }
        if (!backward && !forward) {
            ctx.addDifference(none, original, updated);
        }
    }
}
 
Example #17
Source Project: apicurio-registry   Author: Apicurio   File: ConditionalSchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitThenSchema(SchemaWrapper thenSchema) {
    Schema o = original.getThenSchema().orElse(null);
    compareSchema(ctx.sub("then"), o, thenSchema.getWrapped(),
        CONDITIONAL_TYPE_THEN_SCHEMA_ADDED,
        CONDITIONAL_TYPE_THEN_SCHEMA_REMOVED,
        CONDITIONAL_TYPE_THEN_SCHEMA_COMPATIBLE_BOTH,
        CONDITIONAL_TYPE_THEN_SCHEMA_COMPATIBLE_BACKWARD_NOT_FORWARD,
        CONDITIONAL_TYPE_THEN_SCHEMA_COMPATIBLE_FORWARD_NOT_BACKWARD,
        CONDITIONAL_TYPE_THEN_SCHEMA_COMPATIBLE_NONE);
    super.visitThenSchema(thenSchema);
}
 
Example #18
Source Project: apicurio-registry   Author: Apicurio   File: ConditionalSchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitElseSchema(SchemaWrapper elseSchema) {
    Schema o = original.getElseSchema().orElse(null);
    compareSchema(ctx.sub("else"), o, elseSchema.getWrapped(),
        CONDITIONAL_TYPE_ELSE_SCHEMA_ADDED,
        CONDITIONAL_TYPE_ELSE_SCHEMA_REMOVED,
        CONDITIONAL_TYPE_ELSE_SCHEMA_COMPATIBLE_BOTH,
        CONDITIONAL_TYPE_ELSE_SCHEMA_COMPATIBLE_BACKWARD_NOT_FORWARD,
        CONDITIONAL_TYPE_ELSE_SCHEMA_COMPATIBLE_FORWARD_NOT_BACKWARD,
        CONDITIONAL_TYPE_ELSE_SCHEMA_COMPATIBLE_NONE);
    super.visitElseSchema(elseSchema);
}
 
Example #19
Source Project: molgenis   Author: molgenis   File: JsonValidator.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Validates that a JSON string conforms to a {@link Schema}.
 *
 * @param json the JSON string to check
 * @param schema the {@link Schema} that the JSON string should conform to
 * @throws JsonValidationException if the JSON doesn't conform to the schema, containing a {@link
 *     ConstraintViolation} for each message
 */
public void validate(String json, Schema schema) {
  try {
    schema.validate(new JSONObject(json));
  } catch (ValidationException validationException) {
    throw new JsonValidationException(
        validationException.getAllMessages().stream()
            .map(ConstraintViolation::new)
            .collect(toSet()));
  }
}
 
Example #20
Source Project: apicurio-registry   Author: Apicurio   File: SchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitStringSchema(StringSchemaWrapper stringSchema) {
    Schema subschema = getCompatibleSubschemaOrOriginal(original, stringSchema); // In case of enum

    if (subschema instanceof FalseSchema)
        return; // FalseSchema matches nothing

    if (!(subschema instanceof StringSchema)) {
        ctx.addDifference(SUBSCHEMA_TYPE_CHANGED, subschema, stringSchema);
        return;
    }
    // ctx is assumed to already contain the path
    stringSchema.accept(new StringSchemaDiffVisitor(ctx, (StringSchema) subschema));
}
 
Example #21
Source Project: json-schema   Author: everit-org   File: ReferenceLookup.java    License: 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 #22
Source Project: apicurio-registry   Author: Apicurio   File: SchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitNumberSchema(NumberSchemaWrapper schema) {
    Schema subschema = getCompatibleSubschemaOrOriginal(original, schema); // In case of enum

    if (subschema instanceof FalseSchema)
        return; // FalseSchema matches nothing

    if (!(subschema instanceof NumberSchema)) {
        ctx.addDifference(SUBSCHEMA_TYPE_CHANGED, subschema, schema);
        return;
    }
    schema.accept(new NumberSchemaDiffVisitor(ctx, (NumberSchema) subschema));
}
 
Example #23
Source Project: apicurio-registry   Author: Apicurio   File: ObjectSchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void visitPatternPropertySchema(Pattern propertyNamePattern, SchemaWrapper schema) {
    final Map<String, Schema> stringifiedOriginal = original.getPatternProperties().entrySet().stream()
        .collect(toMap(e -> e.getKey().toString(), Entry::getValue)); // TODO maybe add a wrapper class for Pattern

    if (stringifiedOriginal.containsKey(propertyNamePattern.toString())) {
        schema.accept(new SchemaDiffVisitor(ctx.sub("patternProperties/" + propertyNamePattern),
            stringifiedOriginal.get(propertyNamePattern.toString())));
    }
    super.visitPatternPropertySchema(propertyNamePattern, schema);
}
 
Example #24
Source Project: apicurio-registry   Author: Apicurio   File: WrapUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static SchemaWrapper wrap(Schema schema) {
    if (schema == null)
        return null;

    if (schema instanceof ObjectSchema) {
        return new ObjectSchemaWrapper((ObjectSchema) schema);
    } else if (schema instanceof ArraySchema) {
        return new ArraySchemaWrapper((ArraySchema) schema);
    } else if (schema instanceof StringSchema) {
        return new StringSchemaWrapper((StringSchema) schema);
    } else if (schema instanceof EmptySchema && !(schema instanceof TrueSchema)) {
        return new EmptySchemaWrapper((EmptySchema) schema);
    } else if (schema instanceof TrueSchema) {
        return new TrueSchemaWrapper((TrueSchema) schema);
    } else if (schema instanceof FalseSchema) {
        return new FalseSchemaWrapper((FalseSchema) schema);
    } else if (schema instanceof BooleanSchema) {
        return new BooleanSchemaWrapper((BooleanSchema) schema);
    } else if (schema instanceof ConstSchema) {
        return new ConstSchemaWrapper((ConstSchema) schema);
    } else if (schema instanceof EnumSchema) {
        return new EnumSchemaWrapper((EnumSchema) schema);
    } else if (schema instanceof NullSchema) {
        return new NullSchemaWrapper((NullSchema) schema);
    } else if (schema instanceof NotSchema) {
        return new NotSchemaWrapper((NotSchema) schema);
    } else if (schema instanceof ReferenceSchema) {
        return new ReferenceSchemaWrapper((ReferenceSchema) schema);
    } else if (schema instanceof CombinedSchema) {
        return new CombinedSchemaWrapper((CombinedSchema) schema);
    } else if (schema instanceof ConditionalSchema) {
        return new ConditionalSchemaWrapper((ConditionalSchema) schema);
    } else if (schema instanceof NumberSchema) {
        return new NumberSchemaWrapper((NumberSchema) schema);
    } else {
        throw new IllegalStateException("No wrapper for an underlying schema type '" + schema.getClass() + "': " + schema);
    }
}
 
Example #25
Source Project: apicurio-registry   Author: Apicurio   File: WrapUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static <K> Map<K, SchemaWrapper> wrap(Map<K, Schema> map) {
    requireNonNull(map);
    return map.entrySet().stream()
        //.map(entry -> new SimpleEntry<>(entry.getKey(), wrap(entry.getValue())))
        .collect(toMap(
            Entry::getKey,
            e -> wrap(e.getValue())
        ));
}
 
Example #26
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: Apache License 2.0 5 votes vote down vote up
@Override List<Schema.Builder<?>> extract() {
    if (containsKey("not")) {
        Schema mustNotMatch = defaultLoader.loadChild(require("not")).build();
        return singletonList(NotSchema.builder().mustNotMatch(mustNotMatch));
    }
    return emptyList();
}
 
Example #27
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: Apache License 2.0 5 votes vote down vote up
private ConditionalSchema.Builder buildConditionalSchema() {
    ConditionalSchema.Builder builder = ConditionalSchema.builder();
    maybe("if").map(defaultLoader::loadChild).map(Schema.Builder::build).ifPresent(builder::ifSchema);
    maybe("then").map(defaultLoader::loadChild).map(Schema.Builder::build).ifPresent(builder::thenSchema);
    maybe("else").map(defaultLoader::loadChild).map(Schema.Builder::build).ifPresent(builder::elseSchema);
    return builder;
}
 
Example #28
Source Project: aws-cloudformation-resource-schema   Author: aws-cloudformation   File: ValidatorRefResolutionTests.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * model that contains an invalid value in one of its properties fails
 * validation
 */
@Test
public void validateModel_containsBadRef_shoudThrow() {
    JSONObject resourceDefinition = loadJSON(RESOURCE_DEFINITION_PATH);
    Schema rawSchema = validator.loadResourceDefinitionSchema(resourceDefinition);
    ResourceTypeSchema schema = new ResourceTypeSchema(rawSchema);

    final JSONObject template = getValidModelWithRefs();
    // make the model invalid by adding a property containing a malformed IP address
    template.put("propertyB", "not.an.IP.address");

    assertThatExceptionOfType(org.everit.json.schema.ValidationException.class)
        .isThrownBy(() -> schema.validate(template));
}
 
Example #29
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: 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 #30
Source Project: json-schema   Author: everit-org   File: ReadWriteContextLoadingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void worksOnlyInV7Mode() {
    ObjectSchema rootSchema = (ObjectSchema) loadAsV6(LOADER.readObj("read-write-context.json"));
    Schema readOnlyProp = rootSchema.getPropertySchemas().get("readOnlyProp");
    Schema writeOnlyProp = rootSchema.getPropertySchemas().get("writeOnlyProp");
    assertNull(readOnlyProp.isReadOnly());
    assertNull(writeOnlyProp.isWriteOnly());
}