-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from sebastian-toepfer/vocabulary
add vocabulary
- Loading branch information
Showing
7 changed files
with
269 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/Vocabulary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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.core; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.core.keyword.KeywordType; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
/** | ||
* see: https://json-schema.org/draft/2020-12/json-schema-core.html#name-schema-vocabularies | ||
*/ | ||
public interface Vocabulary { | ||
URI id(); | ||
|
||
Optional<KeywordType> findKeywordTypeByName(String name); | ||
} |
70 changes: 70 additions & 0 deletions
70
...rc/main/java/io/github/sebastiantoepfer/jsonschema/core/impl/keyword/BasicVocabulary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.core.impl.keyword; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.core.Vocabulary; | ||
import io.github.sebastiantoepfer.jsonschema.core.keyword.DefaultAnnotation; | ||
import io.github.sebastiantoepfer.jsonschema.core.keyword.Keyword; | ||
import io.github.sebastiantoepfer.jsonschema.core.keyword.KeywordType; | ||
import jakarta.json.JsonValue; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
final class BasicVocabulary implements Vocabulary { | ||
|
||
@Override | ||
public URI id() { | ||
return URI.create("http://https://github.com/sebastian-toepfer/json-schema/basic"); | ||
} | ||
|
||
@Override | ||
public Optional<KeywordType> findKeywordTypeByName(final String name) { | ||
KeywordType keywordType = | ||
switch (name) { | ||
case "type" -> new KeywordType() { | ||
@Override | ||
public String name() { | ||
return "type"; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(final JsonValue value) { | ||
return new Type(value); | ||
} | ||
}; | ||
default -> new KeywordType() { | ||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(final JsonValue value) { | ||
return new DefaultAnnotation(name(), value); | ||
} | ||
}; | ||
}; | ||
return Optional.of(keywordType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/KeywordType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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.core.keyword; | ||
|
||
import jakarta.json.JsonValue; | ||
import java.util.Objects; | ||
|
||
public interface KeywordType { | ||
default boolean hasName(String name) { | ||
return Objects.equals(name(), name); | ||
} | ||
|
||
String name(); | ||
|
||
Keyword createKeyword(JsonValue value); | ||
} |
59 changes: 59 additions & 0 deletions
59
...est/java/io/github/sebastiantoepfer/jsonschema/core/impl/keyword/BasicVocabularyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.core.impl.keyword; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import com.github.npathai.hamcrestopt.OptionalMatchers; | ||
import io.github.sebastiantoepfer.jsonschema.core.keyword.KeywordType; | ||
import java.net.URI; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BasicVocabularyTest { | ||
|
||
@Test | ||
void should_return_a_fake_id() { | ||
assertThat( | ||
new BasicVocabulary().id(), | ||
is(URI.create("http://https://github.com/sebastian-toepfer/json-schema/basic")) | ||
); | ||
} | ||
|
||
@Test | ||
void should_return_the_type_keywordtype() { | ||
assertThat( | ||
new BasicVocabulary().findKeywordTypeByName("type").map(KeywordType::name), | ||
OptionalMatchers.isPresentAndIs("type") | ||
); | ||
} | ||
|
||
@Test | ||
void should_return_the_custom_annotation_for_unknow_keyword() { | ||
assertThat( | ||
new BasicVocabulary().findKeywordTypeByName("unknow").map(KeywordType::name), | ||
OptionalMatchers.isPresentAndIs("unknow") | ||
); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/KeywordTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.core.keyword; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import jakarta.json.JsonValue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class KeywordTypeTest { | ||
|
||
@Test | ||
void should_know_his_name() { | ||
assertThat(new TestKeywordType().hasName("test"), is(true)); | ||
assertThat(new TestKeywordType().hasName("unknown"), is(false)); | ||
} | ||
|
||
private static class TestKeywordType implements KeywordType { | ||
|
||
@Override | ||
public String name() { | ||
return "test"; | ||
} | ||
|
||
@Override | ||
public Keyword createKeyword(JsonValue value) { | ||
throw new UnsupportedOperationException("Not supported yet."); | ||
} | ||
} | ||
} |