WEB-37393 If-then-else validation issue

allow both then-else branches only for code completion
This commit is contained in:
Anton Lobov
2019-03-01 12:24:57 +01:00
parent ee5fac3ac2
commit 05f7b40adf
4 changed files with 52 additions and 6 deletions
@@ -279,7 +279,7 @@ class JsonSchemaAnnotatorChecker {
}
final JsonPointerPosition step = JsonPointerPosition.createSingleProperty(name);
final Pair<ThreeState, JsonSchemaObject> pair = doSingleStep(step, schema, true);
final Pair<ThreeState, JsonSchemaObject> pair = doSingleStep(step, schema, true, false);
if (ThreeState.NO.equals(pair.getFirst()) && !set.contains(name)) {
error(JsonBundle.message("json.schema.annotation.not.allowed.property", name), property.getDelegate(),
JsonValidationError.FixableIssueKind.ProhibitedProperty,
@@ -59,7 +59,7 @@ public class JsonSchemaVariantsTreeBuilder {
node.nothingChild();
continue;
}
final Pair<ThreeState, JsonSchemaObject> pair = doSingleStep(step, node.getSchema(), acceptAdditional);
final Pair<ThreeState, JsonSchemaObject> pair = doSingleStep(step, node.getSchema(), acceptAdditional, true);
if (ThreeState.NO.equals(pair.getFirst())) node.nothingChild();
else if (ThreeState.YES.equals(pair.getFirst())) node.anyChild();
else {
@@ -112,10 +112,11 @@ public class JsonSchemaVariantsTreeBuilder {
@NotNull
public static Pair<ThreeState, JsonSchemaObject> doSingleStep(@NotNull JsonPointerPosition step,
@NotNull JsonSchemaObject parent,
boolean acceptAdditionalPropertiesSchemas) {
boolean acceptAdditionalPropertiesSchemas,
boolean processAllBranches) {
final String name = step.getFirstName();
if (name != null) {
return propertyStep(name, parent, acceptAdditionalPropertiesSchemas);
return propertyStep(name, parent, acceptAdditionalPropertiesSchemas, processAllBranches);
} else {
final int index = step.getFirstIndex();
assert index >= 0;
@@ -383,7 +384,8 @@ public class JsonSchemaVariantsTreeBuilder {
@NotNull
private static Pair<ThreeState, JsonSchemaObject> propertyStep(@NotNull String name,
@NotNull JsonSchemaObject parent,
boolean acceptAdditionalPropertiesSchemas) {
boolean acceptAdditionalPropertiesSchemas,
boolean processAllBranches) {
final JsonSchemaObject child = parent.getProperties().get(name);
if (child != null) {
return Pair.create(ThreeState.UNSURE, child);
@@ -398,7 +400,7 @@ public class JsonSchemaVariantsTreeBuilder {
}
List<IfThenElse> ifThenElseList = parent.getIfThenElse();
if (ifThenElseList != null) {
if (ifThenElseList != null && processAllBranches) {
for (IfThenElse ifThenElse : ifThenElseList) {
// resolve inside V7 if-then-else conditionals
JsonSchemaObject childObject;
@@ -1086,4 +1086,28 @@ public class JsonSchemaHighlightingTest extends JsonSchemaHighlightingTestBase {
" }\n" +
" }}", "{ <weak_warning descr=\"Property 'myPropertyXxx' is deprecated: Baz\">\"myPropertyXxx\"</weak_warning>: \"a\" }");
}
public void testIfThenElseFlat() throws Exception {
@Language("JSON") String schemaText = FileUtil.loadFile(new File(getTestDataPath() + "/ifThenElseFlatSchema.json"));
doTest(schemaText, "{\n" +
" \"street_address\": \"24 Sussex Drive\",\n" +
" \"country\": \"Canada\",\n" +
" \"postal_code\": \"K1M 1M4\" \n" +
"}");
doTest(schemaText, "{\n" +
" \"street_address\": \"24 Sussex Drive\",\n" +
" \"country\": \"Canada\",\n" +
" \"postal_code\": <warning descr=\"String is violating the pattern: '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]'\">\"1K1M1M4\"</warning> \n" +
"}");
doTest(schemaText, "{\n" +
" \"street_address\": \"24 Madison Cube Garden NYC\",\n" +
" \"country\": \"United States of America\",\n" +
" \"postal_code\": \"11222-1111-1111\"\n" +
"}");
doTest(schemaText, "{\n" +
" \"street_address\": \"24 Madison Cube Garden NYC\",\n" +
" \"country\": \"United States of America\",\n" +
" \"postal_code\": <warning descr=\"String is violating the pattern: '[0-9]{5}(-[0-9]{4})?'\">\"1-1111-1111\"</warning>\n" +
"}");
}
}
@@ -0,0 +1,20 @@
{
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"country": {
"enum": ["United States of America", "Canada"]
}
},
"if": {
"properties": { "country": { "const": "United States of America" } }
},
"then": {
"properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
},
"else": {
"properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
}
}