Search in sources :

Example 1 with RuleViolationException

use of io.apicurio.registry.rules.RuleViolationException in project apicurio-registry by Apicurio.

the class JsonSchemaContentValidator method validate.

/**
 * @see io.apicurio.registry.rules.validity.ContentValidator#validate(io.apicurio.registry.rules.validity.ValidityLevel, ContentHandle)
 */
@Override
public void validate(ValidityLevel level, ContentHandle artifactContent) throws RuleViolationException {
    if (level == ValidityLevel.SYNTAX_ONLY || level == ValidityLevel.FULL) {
        try {
            JsonNode node = objectMapper.readTree(artifactContent.bytes());
            if (level == ValidityLevel.FULL) {
                JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
                factory.getSchema(node);
            }
        } catch (Exception e) {
            throw new RuleViolationException("Syntax violation for JSON Schema artifact.", RuleType.VALIDITY, level.name(), e);
        }
    }
}
Also used : JsonSchemaFactory(com.networknt.schema.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) RuleViolationException(io.apicurio.registry.rules.RuleViolationException) RuleViolationException(io.apicurio.registry.rules.RuleViolationException)

Example 2 with RuleViolationException

use of io.apicurio.registry.rules.RuleViolationException in project apicurio-registry by Apicurio.

the class KafkaConnectContentValidator method validate.

/**
 * @see io.apicurio.registry.rules.validity.ContentValidator#validate(io.apicurio.registry.rules.validity.ValidityLevel, io.apicurio.registry.content.ContentHandle)
 */
@Override
public void validate(ValidityLevel level, ContentHandle artifactContent) throws RuleViolationException {
    if (level == ValidityLevel.SYNTAX_ONLY || level == ValidityLevel.FULL) {
        try {
            JsonNode jsonNode = mapper.readTree(artifactContent.content());
            jsonConverter.asConnectSchema(jsonNode);
        } catch (Exception e) {
            throw new RuleViolationException("Syntax violation for Kafka Connect Schema artifact.", RuleType.VALIDITY, level.name(), e);
        }
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RuleViolationException(io.apicurio.registry.rules.RuleViolationException) RuleViolationException(io.apicurio.registry.rules.RuleViolationException)

Example 3 with RuleViolationException

use of io.apicurio.registry.rules.RuleViolationException in project apicurio-registry by Apicurio.

the class XsdContentValidator method validate.

/**
 * @see io.apicurio.registry.rules.validity.ContentValidator#validate(io.apicurio.registry.rules.validity.ValidityLevel,
 *      io.apicurio.registry.content.ContentHandle)
 */
@Override
public void validate(ValidityLevel level, ContentHandle artifactContent) throws RuleViolationException {
    super.validate(level, artifactContent);
    if (level == ValidityLevel.FULL) {
        try (InputStream semanticStream = artifactContent.stream()) {
            // validate that its a valid schema
            Source source = new StreamSource(semanticStream);
            SchemaFactoryAccessor.getSchemaFactory().newSchema(source);
        } catch (Exception e) {
            throw new RuleViolationException("Syntax violation for XSD Schema artifact.", RuleType.VALIDITY, level.name(), e);
        }
    }
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) RuleViolationException(io.apicurio.registry.rules.RuleViolationException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) RuleViolationException(io.apicurio.registry.rules.RuleViolationException)

Example 4 with RuleViolationException

use of io.apicurio.registry.rules.RuleViolationException in project apicurio-registry by Apicurio.

the class XsdContentValidator method validate.

/**
 * @see io.apicurio.registry.rules.validity.ContentValidator#validate(ValidityLevel, ContentHandle, Map)
 */
@Override
public void validate(ValidityLevel level, ContentHandle artifactContent, Map<String, ContentHandle> resolvedReferences) throws RuleViolationException {
    super.validate(level, artifactContent, resolvedReferences);
    if (level == ValidityLevel.FULL) {
        try (InputStream semanticStream = artifactContent.stream()) {
            // validate that its a valid schema
            Source source = new StreamSource(semanticStream);
            SchemaFactoryAccessor.getSchemaFactory().newSchema(source);
        } catch (Exception e) {
            throw new RuleViolationException("Syntax violation for XSD Schema artifact.", RuleType.VALIDITY, level.name(), e);
        }
    }
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) RuleViolationException(io.apicurio.registry.rules.RuleViolationException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) RuleViolationException(io.apicurio.registry.rules.RuleViolationException)

Example 5 with RuleViolationException

use of io.apicurio.registry.rules.RuleViolationException in project apicurio-registry by Apicurio.

the class JsonSchemaContentValidatorTest method testInvalidJsonSchemaFull.

@Test
public void testInvalidJsonSchemaFull() throws Exception {
    ContentHandle content = resourceToContentHandle("bad-json-schema-v1.json");
    JsonSchemaContentValidator validator = new JsonSchemaContentValidator();
    RuleViolationException error = Assertions.assertThrows(RuleViolationException.class, () -> {
        validator.validate(ValidityLevel.FULL, content, Collections.emptyMap());
    });
    Assertions.assertFalse(error.getCauses().isEmpty());
    Assertions.assertEquals("expected type: Number, found: Boolean", error.getCauses().iterator().next().getDescription());
    Assertions.assertEquals("#/items/properties/price/exclusiveMinimum", error.getCauses().iterator().next().getContext());
}
Also used : ContentHandle(io.apicurio.registry.content.ContentHandle) RuleViolationException(io.apicurio.registry.rules.RuleViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

RuleViolationException (io.apicurio.registry.rules.RuleViolationException)17 ContentHandle (io.apicurio.registry.content.ContentHandle)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ConflictException (io.apicurio.registry.ccompat.rest.error.ConflictException)3 UnprocessableEntityException (io.apicurio.registry.ccompat.rest.error.UnprocessableEntityException)3 ArtifactMetaDataDto (io.apicurio.registry.storage.dto.ArtifactMetaDataDto)3 RuleViolation (io.apicurio.registry.rules.RuleViolation)2 InputStream (java.io.InputStream)2 Source (javax.xml.transform.Source)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Schema (org.apache.avro.Schema)2 Test (org.junit.jupiter.api.Test)2 Descriptors (com.google.protobuf.Descriptors)1 JsonSchemaFactory (com.networknt.schema.JsonSchemaFactory)1 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)1 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)1 Authorized (io.apicurio.registry.auth.Authorized)1 SchemaId (io.apicurio.registry.cncf.schemaregistry.beans.SchemaId)1 Error (io.apicurio.registry.rest.v2.beans.Error)1 RuleViolationError (io.apicurio.registry.rest.v2.beans.RuleViolationError)1