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

The following examples show how to use org.everit.json.schema.BooleanSchema. 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: 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 #2
Source Project: apicurio-registry   Author: Apicurio   File: SchemaDiffVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitBooleanSchema(BooleanSchemaWrapper schema) {
    Schema subschema = getCompatibleSubschemaOrOriginal(original, schema); // In case of enum

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

    if (!(subschema instanceof BooleanSchema)) {
        ctx.addDifference(SUBSCHEMA_TYPE_CHANGED, subschema, schema);
        return;
    }
    schema.accept(new BooleanSchemaDiffVisitor(ctx, (BooleanSchema) subschema));
}
 
Example #3
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 #4
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 #5
Source Project: json-schema   Author: everit-org   File: CombinedSchemaLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void multipleCombinedSchemasAtTheSameNestingLevel() {
    SchemaLoader defaultLoader = SchemaLoader.builder().schemaJson(get("multipleKeywords")).build();
    JsonObject json = JsonValue.of(get("multipleKeywords")).requireObject();
    new LoadingState(LoaderConfig.defaultV4Config(), emptyMap(), json, json, null, SchemaLocation.empty());
    CombinedSchemaLoader subject = new CombinedSchemaLoader(defaultLoader);
    Set<Schema> actual = new HashSet<>(
            subject.extract(json).extractedSchemas.stream().map(builder -> builder.build()).collect(toList()));
    HashSet<CombinedSchema> expected = new HashSet<>(asList(
            CombinedSchema.allOf(singletonList(BooleanSchema.INSTANCE)).build(),
            CombinedSchema.anyOf(singletonList(StringSchema.builder().build())).build()
    ));
    assertEquals(expected, actual);
}
 
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 tupleSchema() {
    ArraySchema actual = (ArraySchema) SchemaLoader.load(get("tupleSchema"));
    assertFalse(actual.permitsAdditionalItems());
    assertNull(actual.getAllItemSchema());
    assertEquals(2, actual.getItemSchemas().size());
    assertEquals(BooleanSchema.INSTANCE, actual.getItemSchemas().get(0));
    assertEquals(NullSchema.INSTANCE, actual.getItemSchemas().get(1));
}
 
Example #7
Source Project: json-schema   Author: everit-org   File: DefinesPropertyTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void definesPropertyIfSubschemaMatchCountIsAcceptedByCriterion() {
    CombinedSchema subject = CombinedSchema.builder()
            .subschema(ObjectSchema.builder().addPropertySchema("a", BooleanSchema.INSTANCE).build())
            .subschema(ObjectSchema.builder().addPropertySchema("b", BooleanSchema.INSTANCE).build())
            .criterion((subschemaCount, matchingSubschemaCount) -> {
                if (matchingSubschemaCount == 1 && subschemaCount == 2) {
                    // dummy exception
                    throw new ValidationException(Object.class, new Object());
                }
            })
            .build();
    assertFalse(subject.definesProperty("a"));
}
 
Example #8
Source Project: apicurio-registry   Author: Apicurio   File: BooleanSchemaDiffVisitor.java    License: Apache License 2.0 4 votes vote down vote up
public BooleanSchemaDiffVisitor(DiffContext ctx, BooleanSchema original) {
    this.ctx = ctx;
    this.original = original;
}
 
Example #9
Source Project: apicurio-registry   Author: Apicurio   File: BooleanSchemaWrapper.java    License: Apache License 2.0 4 votes vote down vote up
public BooleanSchemaWrapper(BooleanSchema wrapped) {
    this.wrapped = wrapped;
}
 
Example #10
Source Project: json-schema   Author: everit-org   File: SchemaLoaderTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void booleanSchema() {
    BooleanSchema actual = (BooleanSchema) SchemaLoader.load(get("booleanSchema"));
    assertNotNull(actual);
}