-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display user friendly messages for Json Schema validation failures (#…
…1662) * Display user friendly messages for Json Schema validation failures * Review Comments Co-authored-by: Rishi Agarwal <rishi.agarwal@verizonmedia.com>
- Loading branch information
1 parent
a369eaf
commit bff9eae
Showing
20 changed files
with
1,009 additions
and
489 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
.../java/com/yahoo/elide/contrib/dynamicconfighelpers/DraftV4LibraryWithElideFormatAttr.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,50 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.contrib.dynamicconfighelpers; | ||
|
||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideCardinalityFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideFieldNameFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideFieldTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideGrainTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideJoinTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideNameFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideTimeFieldTypeFormatAttr; | ||
|
||
import com.github.fge.jsonschema.library.DraftV4Library; | ||
import com.github.fge.jsonschema.library.Library; | ||
import com.github.fge.jsonschema.library.LibraryBuilder; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) | ||
public class DraftV4LibraryWithElideFormatAttr { | ||
private static Library LIBRARY = null; | ||
|
||
public static Library getInstance() { | ||
if (LIBRARY == null) { | ||
LibraryBuilder builder = DraftV4Library.get().thaw(); | ||
|
||
builder.addFormatAttribute(ElideFieldNameFormatAttr.FORMAT_NAME, | ||
ElideFieldNameFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideCardinalityFormatAttr.FORMAT_NAME, | ||
ElideCardinalityFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideFieldTypeFormatAttr.FORMAT_NAME, | ||
ElideFieldTypeFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideGrainTypeFormatAttr.FORMAT_NAME, | ||
ElideGrainTypeFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideJoinTypeFormatAttr.FORMAT_NAME, | ||
ElideJoinTypeFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideTimeFieldTypeFormatAttr.FORMAT_NAME, | ||
ElideTimeFieldTypeFormatAttr.getInstance()); | ||
builder.addFormatAttribute(ElideNameFormatAttr.FORMAT_NAME, | ||
ElideNameFormatAttr.getInstance()); | ||
|
||
LIBRARY = builder.freeze(); | ||
} | ||
|
||
return LIBRARY; | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
...ain/java/com/yahoo/elide/contrib/dynamicconfighelpers/MessageBundleWithElideMessages.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,48 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.contrib.dynamicconfighelpers; | ||
|
||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideCardinalityFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideFieldNameFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideFieldTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideGrainTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideJoinTypeFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideNameFormatAttr; | ||
import com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats.ElideTimeFieldTypeFormatAttr; | ||
|
||
import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
import com.github.fge.msgsimple.load.MessageBundles; | ||
import com.github.fge.msgsimple.source.MapMessageSource; | ||
import com.github.fge.msgsimple.source.MapMessageSource.Builder; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) | ||
public class MessageBundleWithElideMessages { | ||
private static MessageBundle BUNDLE = null; | ||
|
||
public static MessageBundle getInstance() { | ||
if (BUNDLE == null) { | ||
Builder msgSourceBuilder = MapMessageSource.newBuilder(); | ||
|
||
msgSourceBuilder.put(ElideFieldNameFormatAttr.FORMAT_KEY, ElideFieldNameFormatAttr.FORMAT_MSG); | ||
msgSourceBuilder.put(ElideFieldNameFormatAttr.NAME_KEY, ElideFieldNameFormatAttr.NAME_MSG); | ||
msgSourceBuilder.put(ElideCardinalityFormatAttr.TYPE_KEY, ElideCardinalityFormatAttr.TYPE_MSG); | ||
msgSourceBuilder.put(ElideFieldTypeFormatAttr.TYPE_KEY, ElideFieldTypeFormatAttr.TYPE_MSG); | ||
msgSourceBuilder.put(ElideGrainTypeFormatAttr.TYPE_KEY, ElideGrainTypeFormatAttr.TYPE_MSG); | ||
msgSourceBuilder.put(ElideJoinTypeFormatAttr.TYPE_KEY, ElideJoinTypeFormatAttr.TYPE_MSG); | ||
msgSourceBuilder.put(ElideTimeFieldTypeFormatAttr.TYPE_KEY, ElideTimeFieldTypeFormatAttr.TYPE_MSG); | ||
msgSourceBuilder.put(ElideNameFormatAttr.FORMAT_KEY, ElideNameFormatAttr.FORMAT_MSG); | ||
|
||
BUNDLE = MessageBundles.getBundle(JsonSchemaValidationBundle.class).thaw() | ||
.appendSource(msgSourceBuilder.build()) | ||
.freeze(); | ||
} | ||
|
||
return BUNDLE; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../com/yahoo/elide/contrib/dynamicconfighelpers/jsonformats/ElideCardinalityFormatAttr.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,42 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats; | ||
|
||
import com.github.fge.jackson.NodeType; | ||
import com.github.fge.jsonschema.core.exceptions.ProcessingException; | ||
import com.github.fge.jsonschema.core.report.ProcessingReport; | ||
import com.github.fge.jsonschema.format.AbstractFormatAttribute; | ||
import com.github.fge.jsonschema.format.FormatAttribute; | ||
import com.github.fge.jsonschema.processors.data.FullData; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
|
||
public class ElideCardinalityFormatAttr extends AbstractFormatAttribute { | ||
private static final FormatAttribute INSTANCE = new ElideCardinalityFormatAttr(); | ||
private static final String CARDINALITY_REGEX = "^(?i)(Tiny|Small|Medium|Large|Huge)$"; | ||
|
||
public static final String FORMAT_NAME = "elideCardiality"; | ||
public static final String TYPE_KEY = "elideCardiality.error.enum"; | ||
public static final String TYPE_MSG = "Cardinality type [%s] is not allowed. Supported value is one of " | ||
+ "[Tiny, Small, Medium, Large, Huge]."; | ||
|
||
private ElideCardinalityFormatAttr() { | ||
super(FORMAT_NAME, NodeType.STRING); | ||
} | ||
|
||
public static FormatAttribute getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) | ||
throws ProcessingException { | ||
final String input = data.getInstance().getNode().textValue(); | ||
|
||
if (!input.matches(CARDINALITY_REGEX)) { | ||
report.error(newMsg(data, bundle, TYPE_KEY).putArgument("value", input)); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...va/com/yahoo/elide/contrib/dynamicconfighelpers/jsonformats/ElideFieldNameFormatAttr.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,48 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats; | ||
|
||
import com.github.fge.jackson.NodeType; | ||
import com.github.fge.jsonschema.core.exceptions.ProcessingException; | ||
import com.github.fge.jsonschema.core.report.ProcessingReport; | ||
import com.github.fge.jsonschema.format.AbstractFormatAttribute; | ||
import com.github.fge.jsonschema.format.FormatAttribute; | ||
import com.github.fge.jsonschema.processors.data.FullData; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
|
||
public class ElideFieldNameFormatAttr extends AbstractFormatAttribute { | ||
private static final FormatAttribute INSTANCE = new ElideFieldNameFormatAttr(); | ||
private static final String FIELD_NAME_FORMAT_REGEX = "^[A-Za-z][0-9A-Za-z_]*$"; | ||
|
||
public static final String FORMAT_NAME = "elideFieldName"; | ||
public static final String NAME_KEY = "elideFieldName.error.name"; | ||
public static final String NAME_MSG = "Field name [%s] is not allowed. Field name cannot be 'id'"; | ||
public static final String FORMAT_KEY = "elideFieldName.error.format"; | ||
public static final String FORMAT_MSG = "Field name [%s] is not allowed. Field name must start with " | ||
+ "an alphabet and can include alaphabets, numbers and '_' only."; | ||
|
||
private ElideFieldNameFormatAttr() { | ||
super(FORMAT_NAME, NodeType.STRING); | ||
} | ||
|
||
public static FormatAttribute getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) | ||
throws ProcessingException { | ||
final String input = data.getInstance().getNode().textValue(); | ||
|
||
if (!input.matches(FIELD_NAME_FORMAT_REGEX)) { | ||
report.error(newMsg(data, bundle, FORMAT_KEY).putArgument("value", input)); | ||
} | ||
|
||
if (input.equalsIgnoreCase("id")) { | ||
report.error(newMsg(data, bundle, NAME_KEY).putArgument("value", input)); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...va/com/yahoo/elide/contrib/dynamicconfighelpers/jsonformats/ElideFieldTypeFormatAttr.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,42 @@ | ||
/* | ||
* Copyright 2020, Yahoo Inc. | ||
* Licensed under the Apache License, Version 2.0 | ||
* See LICENSE file in project root for terms. | ||
*/ | ||
package com.yahoo.elide.contrib.dynamicconfighelpers.jsonformats; | ||
|
||
import com.github.fge.jackson.NodeType; | ||
import com.github.fge.jsonschema.core.exceptions.ProcessingException; | ||
import com.github.fge.jsonschema.core.report.ProcessingReport; | ||
import com.github.fge.jsonschema.format.AbstractFormatAttribute; | ||
import com.github.fge.jsonschema.format.FormatAttribute; | ||
import com.github.fge.jsonschema.processors.data.FullData; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
|
||
public class ElideFieldTypeFormatAttr extends AbstractFormatAttribute { | ||
private static final FormatAttribute INSTANCE = new ElideFieldTypeFormatAttr(); | ||
private static final String FIELD_TYPE_REGEX = "^(?i)(Integer|Decimal|Money|Text|Coordinate|Boolean)$"; | ||
|
||
public static final String FORMAT_NAME = "elideFieldType"; | ||
public static final String TYPE_KEY = "elideFieldType.error.enum"; | ||
public static final String TYPE_MSG = "Field type [%s] is not allowed. Supported value is one of " | ||
+ "[Integer, Decimal, Money, Text, Coordinate, Boolean]."; | ||
|
||
private ElideFieldTypeFormatAttr() { | ||
super(FORMAT_NAME, NodeType.STRING); | ||
} | ||
|
||
public static FormatAttribute getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void validate(final ProcessingReport report, final MessageBundle bundle, final FullData data) | ||
throws ProcessingException { | ||
final String input = data.getInstance().getNode().textValue(); | ||
|
||
if (!input.matches(FIELD_TYPE_REGEX)) { | ||
report.error(newMsg(data, bundle, TYPE_KEY).putArgument("value", input)); | ||
} | ||
} | ||
} |
Oops, something went wrong.