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

The following examples show how to use org.everit.json.schema.NumberSchema. 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: 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 #2
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 #3
Source Project: restdocs-raml   Author: ePages-de   File: JsonSchemaFromFieldDescriptorsGenerator.java    License: 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 Project: nakadi   Author: zalando   File: NumberSchemaDiff.java    License: MIT License 5 votes vote down vote up
static void recursiveCheck(final NumberSchema numberSchemaOriginal, final NumberSchema numberSchemaUpdate,
                           final SchemaDiffState state) {
    if (!Objects.equals(numberSchemaOriginal.getMaximum(), numberSchemaUpdate.getMaximum())) {
        state.addChange("maximum", ATTRIBUTE_VALUE_CHANGED);
    } else if (!Objects.equals(numberSchemaOriginal.getMinimum(), numberSchemaUpdate.getMinimum())) {
        state.addChange("minimum", ATTRIBUTE_VALUE_CHANGED);
    } else if (!Objects.equals(numberSchemaOriginal.getMultipleOf(), numberSchemaUpdate.getMultipleOf())) {
        state.addChange("multipleOf", ATTRIBUTE_VALUE_CHANGED);
    }
}
 
Example #5
Source Project: json-schema   Author: everit-org   File: SchemaExtractor.java    License: Apache License 2.0 5 votes vote down vote up
NumberSchema.Builder buildNumberSchema() {
    PropertySnifferSchemaExtractor.NUMBER_SCHEMA_PROPS.forEach(consumedKeys::keyConsumed);
    NumberSchema.Builder builder = NumberSchema.builder();
    maybe("minimum").map(JsonValue::requireNumber).ifPresent(builder::minimum);
    maybe("maximum").map(JsonValue::requireNumber).ifPresent(builder::maximum);
    maybe("multipleOf").map(JsonValue::requireNumber).ifPresent(builder::multipleOf);
    maybe("exclusiveMinimum").ifPresent(exclMin -> exclusiveLimitHandler.handleExclusiveMinimum(exclMin, builder));
    maybe("exclusiveMaximum").ifPresent(exclMax -> exclusiveLimitHandler.handleExclusiveMaximum(exclMax, builder));
    return builder;
}
 
Example #6
Source Project: json-schema   Author: everit-org   File: SchemaLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void integerSchema() {
    NumberSchema actual = (NumberSchema) SchemaLoader.load(get("integerSchema"));
    assertEquals(10, actual.getMinimum().intValue());
    assertEquals(20, actual.getMaximum().intValue());
    assertEquals(5, actual.getMultipleOf().intValue());
    assertTrue(actual.isExclusiveMinimum());
    assertTrue(actual.isExclusiveMaximum());
    assertTrue(actual.requiresInteger());
}
 
Example #7
Source Project: json-schema   Author: everit-org   File: SchemaLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void implicitAnyOfLoadsTypeProps() {
    CombinedSchema schema = (CombinedSchema) SchemaLoader.load(get("multipleTypesWithProps"));
    StringSchema stringSchema = schema.getSubschemas().stream()
            .filter(sub -> sub instanceof StringSchema)
            .map(sub -> (StringSchema) sub)
            .findFirst().orElseThrow(() -> new AssertionError("no StringSchema"));
    NumberSchema numSchema = schema.getSubschemas().stream()
            .filter(sub -> sub instanceof NumberSchema)
            .map(sub -> (NumberSchema) sub)
            .findFirst()
            .orElseThrow(() -> new AssertionError("no NumberSchema"));
    assertEquals(3, stringSchema.getMinLength().intValue());
    assertEquals(5, numSchema.getMinimum().intValue());
}
 
Example #8
Source Project: json-schema   Author: everit-org   File: SchemaLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void pointerResolution() {
    ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
    ObjectSchema rectangleSchema = (ObjectSchema) ((ReferenceSchema) actual.getPropertySchemas()
            .get("rectangle"))
            .getReferredSchema();
    assertNotNull(rectangleSchema);
    ReferenceSchema aRef = (ReferenceSchema) rectangleSchema.getPropertySchemas().get("a");
    assertTrue(aRef.getReferredSchema() instanceof NumberSchema);
}
 
Example #9
Source Project: json-schema   Author: everit-org   File: NumberSchemaLoadingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void v4Attributes() {
    NumberSchema expected = NumberSchema.builder()
            .minimum(5)
            .maximum(10)
            .multipleOf(2)
            .exclusiveMinimum(true)
            .exclusiveMaximum(true)
            .build();
    NumberSchema actual = (NumberSchema) SchemaLoader.load(get("v4Attributes"));

    assertEquals(expected, actual);
}
 
Example #10
Source Project: json-schema   Author: everit-org   File: NumberSchemaLoadingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void v6Attributes() {
    NumberSchema expected = NumberSchema.builder()
            .minimum(5)
            .maximum(10)
            .multipleOf(2)
            .exclusiveMinimum(5)
            .exclusiveMaximum(10)
            .build();

    NumberSchema actual = (NumberSchema) loadAsV6(get("v6Attributes"));

    assertEquals(expected, actual);
}
 
Example #11
Source Project: json-schema   Author: everit-org   File: NumberSchemaLoadingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void v6DoubleLimits() {
    NumberSchema expected = NumberSchema.builder()
            .requiresNumber(true)
            .exclusiveMinimum(5.5)
            .exclusiveMaximum(10.1)
            .build();

    NumberSchema actual = (NumberSchema) loadAsV6(get("v6DoubleLimits"));

    assertEquals(expected, actual);
}
 
Example #12
Source Project: apicurio-registry   Author: Apicurio   File: NumberSchemaDiffVisitor.java    License: Apache License 2.0 4 votes vote down vote up
public NumberSchemaDiffVisitor(DiffContext ctx, NumberSchema original) {
    this.ctx = ctx;
    this.original = original;
}
 
Example #13
Source Project: apicurio-registry   Author: Apicurio   File: NumberSchemaWrapper.java    License: Apache License 2.0 4 votes vote down vote up
public NumberSchemaWrapper(NumberSchema wrapped) {
    this.wrapped = wrapped;
}
 
Example #14
Source Project: nakadi   Author: zalando   File: SchemaDiff.java    License: MIT License 4 votes vote down vote up
static void recursiveCheck(
        final Schema originalIn,
        final Schema updateIn,
        final SchemaDiffState state) {

    if (originalIn == null && updateIn == null) {
        return;
    }

    if (updateIn == null) {
        state.addChange(SCHEMA_REMOVED);
        return;
    }

    if (originalIn == null) {
        state.addChange(SCHEMA_REMOVED);
        return;
    }

    final Schema original;
    final Schema update;
    if (!originalIn.getClass().equals(updateIn.getClass())) {
        // Tricky part. EmptySchema is the same as an empty ObjectSchema.
        if (originalIn instanceof EmptySchema && updateIn instanceof ObjectSchema) {
            original = replaceWithEmptyObjectSchema(originalIn);
            update = updateIn;
        } else if (typeNarrowed(originalIn, updateIn)) {
            state.addChange(TYPE_NARROWED);
            return;
        } else {
            state.addChange(TYPE_CHANGED);
            return;
        }
    } else {
        original = originalIn;
        update = updateIn;
    }

    state.analyzeSchema(originalIn, () -> {
        if (!Objects.equals(original.getId(), update.getId())) {
            state.addChange(ID_CHANGED);
        }

        if (!Objects.equals(original.getTitle(), update.getTitle())) {
            state.addChange(TITLE_CHANGED);
        }

        if (!Objects.equals(original.getDescription(), update.getDescription())) {
            state.addChange(DESCRIPTION_CHANGED);
        }

        if (original instanceof StringSchema) {
            StringSchemaDiff.recursiveCheck((StringSchema) original, (StringSchema) update, state);
        } else if (original instanceof NumberSchema) {
            NumberSchemaDiff.recursiveCheck((NumberSchema) original, (NumberSchema) update, state);
        } else if (original instanceof EnumSchema) {
            EnumSchemaDiff.recursiveCheck((EnumSchema) original, (EnumSchema) update, state);
        } else if (original instanceof CombinedSchema) {
            CombinedSchemaDiff.recursiveCheck((CombinedSchema) original, (CombinedSchema) update, state);
        } else if (original instanceof ObjectSchema) {
            ObjectSchemaDiff.recursiveCheck((ObjectSchema) original, (ObjectSchema) update, state);
        } else if (original instanceof ArraySchema) {
            ArraySchemaDiff.recursiveCheck((ArraySchema) original, (ArraySchema) update, state);
        } else if (original instanceof ReferenceSchema) {
            ReferenceSchemaDiff.recursiveCheck((ReferenceSchema) original, (ReferenceSchema) update, state);
        }
    });
}
 
Example #15
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void handleExclusiveMinimum(JsonValue exclMinimum, NumberSchema.Builder schemaBuilder) {
    schemaBuilder.exclusiveMinimum(exclMinimum.requireBoolean());
}
 
Example #16
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void handleExclusiveMaximum(JsonValue exclMaximum, NumberSchema.Builder schemaBuilder) {
    schemaBuilder.exclusiveMaximum(exclMaximum.requireBoolean());
}
 
Example #17
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void handleExclusiveMinimum(JsonValue exclMinimum, NumberSchema.Builder schemaBuilder) {
    schemaBuilder.exclusiveMinimum(exclMinimum.requireNumber());
}
 
Example #18
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void handleExclusiveMaximum(JsonValue exclMaximum, NumberSchema.Builder schemaBuilder) {
    schemaBuilder.exclusiveMaximum(exclMaximum.requireNumber());
}
 
Example #19
Source Project: json-schema   Author: everit-org   File: NumberSchemaLoadingTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void exclusiveMaximumIntegTest() {
    NumberSchema subject =(NumberSchema) loadAsV6(get("onlyExMax"));
    TestSupport.failureOf(subject).input(3.5).expect();

}
 
Example #20
Source Project: json-schema   Author: everit-org   File: ReferenceLookupTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void schemaVersionChange() {
    when(schemaClient.get("http://localhost/child-ref")).thenReturn(asStream(v4Subschema));
    NumberSchema actual = (NumberSchema) performLookup("#/properties/definitionInRemote");
    assertTrue(actual.isExclusiveMinimum());
}
 
Example #21
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 votes vote down vote up
void handleExclusiveMinimum(JsonValue exclMinimum, NumberSchema.Builder schemaBuilder); 
Example #22
Source Project: json-schema   Author: everit-org   File: ExclusiveLimitHandler.java    License: Apache License 2.0 votes vote down vote up
void handleExclusiveMaximum(JsonValue exclMaximum, NumberSchema.Builder schemaBuilder);