Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add module to support format assertion #49

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
<modules>
<module>api</module>
<module>vocabulary-spi</module>
<module>core</module>
<module>core</module>
<module>vocabulary-format-assertion</module>
</modules>

<dependencyManagement>
Expand Down
87 changes: 87 additions & 0 deletions vocabulary-format-assertion/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.sebastian-toepfer.json-schema</groupId>
<artifactId>json-schema</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>json-schema-vocabulary-format-assertion</artifactId>
<name>Json Schema :: vocabulary :: format assertion</name>

<properties>
<ddd.verison>0.4.0</ddd.verison>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-vocabulary-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>common</artifactId>
<version>${ddd.verison}</version>
</dependency>
<dependency>
<groupId>io.github.sebastian-toepfer.ddd</groupId>
<artifactId>media-core</artifactId>
<version>${ddd.verison}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.npathai</groupId>
<artifactId>hamcrest-optional</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>json-schema-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.parsson</groupId>
<artifactId>parsson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion;

import java.util.Objects;
import java.util.function.Predicate;

public final class DefaultFormatAssertion implements FormatAssertion {

private final String name;
private final Predicate<String> predicate;

public DefaultFormatAssertion(final String name, final Predicate<String> predicate) {
this.name = Objects.requireNonNull(name);
this.predicate = Objects.requireNonNull(predicate);
}

@Override
public String name() {
return name;
}

@Override
public boolean isValidFor(final String value) {
return predicate.test(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion;

public interface FormatAssertion {
String name();

boolean isValidFor(String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion;

import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.Json;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

class FormatAssertionKeywordType implements KeywordType {

private final List<FormatAssertion> formats;

public FormatAssertionKeywordType(final List<FormatAssertion> formats) {
this.formats = List.copyOf(formats);
}

@Override
public String name() {
return "format";
}

@Override
public Keyword createKeyword(final JsonSchema js) {
if (
js.getValueType() == JsonValue.ValueType.OBJECT &&
js.asJsonObject().containsKey(name()) &&
js.asJsonObject().get(name()).getValueType() == JsonValue.ValueType.STRING
) {
return createKeyword(js.asJsonObject().getString(name()));
} else {
throw new IllegalArgumentException("Value must be a string!");
}
}

private FormatAssertionKeyword createKeyword(final String formatName) {
return formats
.stream()
.filter(format -> format.name().equals(formatName))
.map(FormatAssertionKeyword::new)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(String.format("format %s not supported!", formatName)));
}

private class FormatAssertionKeyword implements Annotation, Assertion {

private final FormatAssertion format;

public FormatAssertionKeyword(final FormatAssertion format) {
this.format = format;
}

@Override
public Collection<KeywordCategory> categories() {
return List.of(KeywordCategory.ANNOTATION, KeywordCategory.ASSERTION);
}

@Override
public boolean hasName(final String name) {
return Objects.equals(name(), name);
}

@Override
public JsonValue valueFor(final JsonValue value) {
return Json.createValue(format.name());
}

@Override
public boolean isValidFor(final JsonValue instance) {
return !InstanceType.STRING.isInstance(instance) || format.isValidFor(((JsonString) instance).getString());
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue(name(), format.name());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion;

import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.LazyVocabularies;
import java.net.URI;
import java.util.List;
import java.util.Optional;

public class FormatAssertionVocabulary implements LazyVocabularies, Vocabulary {

private final List<KeywordType> keywords;

public FormatAssertionVocabulary() {
this.keywords =
List.of(new FormatAssertionKeywordType(List.of(new DefaultFormatAssertion("email", s -> s.contains("@")))));
}

@Override
public Optional<Vocabulary> loadVocabularyWithId(final URI id) {
final Optional<Vocabulary> result;
if (id().equals(id)) {
result = Optional.of(this);
} else {
result = Optional.empty();
}
return result;
}

@Override
public URI id() {
return URI.create("https://json-schema.org/draft/2020-12/vocab/format-assertion");
}

@Override
public Optional<KeywordType> findKeywordTypeByName(final String name) {
return keywords.stream().filter(k -> k.hasName(name)).findFirst();
}
}
Loading
Loading