-
-
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.
add support for if-then-else keyword
- Loading branch information
1 parent
86c6dc1
commit baadf17
Showing
15 changed files
with
917 additions
and
2 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
44 changes: 44 additions & 0 deletions
44
core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/LINKTYPE.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,44 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.type; | ||
|
||
import jakarta.json.JsonValue; | ||
import java.util.function.Predicate; | ||
|
||
public enum LINKTYPE { | ||
VALID { | ||
@Override | ||
Predicate<JsonValue> connect(final Predicate<JsonValue> first, final Predicate<JsonValue> second) { | ||
return first.negate().or(second); | ||
} | ||
}, | ||
INVALID { | ||
@Override | ||
Predicate<JsonValue> connect(final Predicate<JsonValue> first, final Predicate<JsonValue> second) { | ||
return first.or(second); | ||
} | ||
}; | ||
|
||
abstract Predicate<JsonValue> connect(Predicate<JsonValue> first, Predicate<JsonValue> second); | ||
} |
70 changes: 70 additions & 0 deletions
70
.../src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/LinkedKeyword.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 2024 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.type; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import jakarta.json.JsonValue; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
final class LinkedKeyword implements Applicator { | ||
|
||
private final Keyword firstChainLink; | ||
private final LINKTYPE linkType; | ||
private final Keyword secondChanLink; | ||
|
||
public LinkedKeyword(final Keyword firstChainLink, final LINKTYPE linkType, final Keyword secondChanLink) { | ||
this.firstChainLink = Objects.requireNonNull(firstChainLink); | ||
this.linkType = Objects.requireNonNull(linkType); | ||
this.secondChanLink = Objects.requireNonNull(secondChanLink); | ||
} | ||
|
||
@Override | ||
public Collection<KeywordCategory> categories() { | ||
final Set<KeywordCategory> result = EnumSet.copyOf(firstChainLink.categories()); | ||
result.addAll(secondChanLink.categories()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return linkType | ||
.connect(firstChainLink.asApplicator()::applyTo, secondChanLink.asApplicator()::applyTo) | ||
.test(instance); | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return secondChanLink.hasName(name); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return secondChanLink.printOn(media); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/LinkedWith.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,77 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.type; | ||
|
||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
public final class LinkedWith implements AffectedBy { | ||
|
||
private final String name; | ||
private final Function<JsonSubSchema, Keyword> keywordCreator; | ||
private final LINKTYPE linkType; | ||
|
||
public LinkedWith( | ||
final String name, | ||
final Function<JsonSubSchema, Keyword> keywordCreator, | ||
final LINKTYPE linkType | ||
) { | ||
this.name = Objects.requireNonNull(name); | ||
this.keywordCreator = Objects.requireNonNull(keywordCreator); | ||
this.linkType = Objects.requireNonNull(linkType); | ||
} | ||
|
||
@Override | ||
public Function<Keyword, Keyword> findAffectedByKeywordIn(final JsonSchema schema) { | ||
return schema | ||
.subSchema(name) | ||
.map(keywordCreator) | ||
.map(k -> (Function<Keyword, Keyword>) new KeywordLink(k, linkType)) | ||
.orElse(ReplacingKeyword::new); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "LinkedWith{" + "name=" + name + '}'; | ||
} | ||
|
||
private static class KeywordLink implements Function<Keyword, Keyword> { | ||
|
||
private final Keyword firstChainLink; | ||
private final LINKTYPE linkType; | ||
|
||
public KeywordLink(final Keyword firstChainLink, final LINKTYPE linkType) { | ||
this.firstChainLink = Objects.requireNonNull(firstChainLink); | ||
this.linkType = Objects.requireNonNull(linkType); | ||
} | ||
|
||
@Override | ||
public Keyword apply(final Keyword secondChanLink) { | ||
return new LinkedKeyword(firstChainLink, linkType, secondChanLink); | ||
} | ||
} | ||
} |
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
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
67 changes: 67 additions & 0 deletions
67
...rc/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ElseKeyword.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,67 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.vocab.applicator; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import jakarta.json.JsonValue; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>else</b> : <i>Schema</i><br/> | ||
* When if is present, and the instance fails to validate against its subschema, then validation succeeds against this | ||
* keyword if the instance successfully validates against this keyword’s subschema.<br/> | ||
* <b>kind</b> | ||
* <ul> | ||
* <li>Applicator</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/applicator/else/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.2.3 | ||
*/ | ||
class ElseKeyword implements Applicator { | ||
|
||
static final String NAME = "else"; | ||
private final JsonSchema schema; | ||
|
||
public ElseKeyword(final JsonSchema schema) { | ||
this.schema = Objects.requireNonNull(schema); | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return Objects.equals(NAME, name); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(NAME, schema); | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return schema.validator().isValid(instance); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/IfKeyword.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,67 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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.vocab.applicator; | ||
|
||
import io.github.sebastiantoepfer.ddd.common.Media; | ||
import io.github.sebastiantoepfer.jsonschema.JsonSchema; | ||
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; | ||
import jakarta.json.JsonValue; | ||
import java.util.Objects; | ||
|
||
/** | ||
* <b>if</b> : <i>Schema</i><br/> | ||
* This keyword declares a condition based on the validation result of the given schema.<br/> | ||
* <br/> | ||
* <b>kind</b> | ||
* <ul> | ||
* <li>Applicator</li> | ||
* </ul> | ||
* | ||
* source: https://www.learnjsonschema.com/2020-12/applicator/if/ | ||
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.2.1 | ||
*/ | ||
final class IfKeyword implements Applicator { | ||
|
||
static final String NAME = "if"; | ||
private final JsonSchema schema; | ||
|
||
public IfKeyword(final JsonSchema schema) { | ||
this.schema = Objects.requireNonNull(schema); | ||
} | ||
|
||
@Override | ||
public boolean applyTo(final JsonValue instance) { | ||
return schema.validator().isValid(instance); | ||
} | ||
|
||
@Override | ||
public boolean hasName(final String name) { | ||
return Objects.equals(NAME, name); | ||
} | ||
|
||
@Override | ||
public <T extends Media<T>> T printOn(final T media) { | ||
return media.withValue(NAME, schema); | ||
} | ||
} |
Oops, something went wrong.