Skip to content

Commit

Permalink
add support for not keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 6, 2024
1 parent b52da05 commit 2a09ac7
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@
<include>**/tests/draft2020-12/minLength.json</include>
<include>**/tests/draft2020-12/minimum.json</include>
<include>**/tests/draft2020-12/minProperties.json</include>
<!-- more than not -> unevaluatedProperties :(
<include>**/tests/draft2020-12/not.json</include>
-->
<include>**/tests/draft2020-12/multipleOf.json</include>
<include>**/tests/draft2020-12/pattern.json</include>
<include>**/tests/draft2020-12/patternProperties.json</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public ApplicatorVocabulary() {
new SchemaArrayKeywordType(AllOfKeyword.NAME, AllOfKeyword::new),
new SchemaArrayKeywordType(AnyOfKeyword.NAME, AnyOfKeyword::new),
new SchemaArrayKeywordType(OneOfKeyword.NAME, OneOfKeyword::new),
new SubSchemaKeywordType(NotKeyword.NAME, NotKeyword::new),
new NamedJsonSchemaKeywordType(PropertiesKeyword.NAME, PropertiesKeyword::new),
new SubSchemaKeywordType(AdditionalPropertiesKeyword.NAME, AdditionalPropertiesKeyword::new),
new NamedJsonSchemaKeywordType(PatternPropertiesKeyword.NAME, PatternPropertiesKeyword::new),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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>not</b> : <i>Schema</i><br/>
* An instance is valid against this keyword if it fails to validate successfully against the schema defined by
* this keyword.<br/>
* <br/>
* <b>kind</b>
* <ul>
* <li>Applicator</li>
* </ul>
*
* source: https://www.learnjsonschema.com/2020-12/applicator/not/
* spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.1.4
*/
final class NotKeyword implements Applicator {

static final String NAME = "not";
private final JsonSchema schema;

public NotKeyword(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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SubSchemaKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

class NotKeywordTest {

@Test
void should_know_his_name() {
final Keyword items = createKeywordFrom(Json.createObjectBuilder().add("not", JsonValue.FALSE).build());

assertThat(items.hasName("not"), is(true));
assertThat(items.hasName("test"), is(false));
}

@Test
void should_be_printable() {
assertThat(
createKeywordFrom(
Json.createObjectBuilder()
.add(
"not",
Json.createObjectBuilder()
.add(
"properties",
Json.createObjectBuilder().add("foo", Json.createObjectBuilder().add("type", "string"))
)
.add("required", Json.createArrayBuilder().add("foo"))
)
.build()
).printOn(new HashMapMedia()),
(Matcher) hasEntry(is("not"), hasKey("properties"))
);
}

@Test
void should_be_valid_if_schema_not_apply() {
assertThat(
createKeywordFrom(Json.createObjectBuilder().add("not", JsonValue.FALSE).build())
.asApplicator()
.applyTo(Json.createObjectBuilder().add("foo", "foo").build()),
is(true)
);
}

@Test
void should_be_invalid_if_schema_apply() {
assertThat(
createKeywordFrom(Json.createObjectBuilder().add("not", JsonValue.TRUE).build())
.asApplicator()
.applyTo(Json.createObjectBuilder().add("foo", 3).add("bar", "bar").build()),
is(false)
);
}

private static Keyword createKeywordFrom(final JsonObject json) {
return new SubSchemaKeywordType("not", NotKeyword::new).createKeyword(
new DefaultJsonSchemaFactory().create(json)
);
}
}

0 comments on commit 2a09ac7

Please sign in to comment.