Skip to content

Commit d86f135

Browse files
authored
feat(schema): support custom error message definition, for issue #1559 (#3904)
* feat(schema): support custom error message definition, for issue #1559 * change 'errorMessage' to 'error'
1 parent 7f1ee6a commit d86f135

File tree

16 files changed

+176
-36
lines changed

16 files changed

+176
-36
lines changed

core/src/main/java/com/alibaba/fastjson2/schema/AllOf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public Type getType() {
7070
}
7171

7272
@Override
73-
public ValidateResult validate(Object value) {
73+
protected ValidateResult validateInternal(Object value) {
7474
for (JSONSchema item : items) {
7575
ValidateResult result = item.validate(value);
7676
if (!result.isSuccess()) {

core/src/main/java/com/alibaba/fastjson2/schema/Any.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public Type getType() {
1515
}
1616

1717
@Override
18-
public ValidateResult validate(Object value) {
18+
protected ValidateResult validateInternal(Object value) {
1919
return SUCCESS;
2020
}
2121
}

core/src/main/java/com/alibaba/fastjson2/schema/AnyOf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Type getType() {
3737
}
3838

3939
@Override
40-
public ValidateResult validate(Object value) {
40+
protected ValidateResult validateInternal(Object value) {
4141
for (JSONSchema item : items) {
4242
ValidateResult result = item.validate(value);
4343
if (result == SUCCESS) {

core/src/main/java/com/alibaba/fastjson2/schema/ArraySchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public Type getType() {
143143
}
144144

145145
@Override
146-
public ValidateResult validate(Object value) {
146+
protected ValidateResult validateInternal(Object value) {
147147
if (value == null) {
148148
return typed ? FAIL_INPUT_NULL : SUCCESS;
149149
}

core/src/main/java/com/alibaba/fastjson2/schema/BooleanSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Type getType() {
1414
}
1515

1616
@Override
17-
public ValidateResult validate(Object value) {
17+
protected ValidateResult validateInternal(Object value) {
1818
if (value == null) {
1919
return FAIL_INPUT_NULL;
2020
}

core/src/main/java/com/alibaba/fastjson2/schema/EnumSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Type getType() {
4040
}
4141

4242
@Override
43-
public ValidateResult validate(Object value) {
43+
protected ValidateResult validateInternal(Object value) {
4444
if (value instanceof BigDecimal) {
4545
BigDecimal decimal = (BigDecimal) value;
4646
value = decimal.stripTrailingZeros();

core/src/main/java/com/alibaba/fastjson2/schema/IntegerSchema.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Type getType() {
6565
}
6666

6767
@Override
68-
public ValidateResult validate(Object value) {
68+
protected ValidateResult validateInternal(Object value) {
6969
if (value == null) {
7070
return typed ? FAIL_INPUT_NULL : SUCCESS;
7171
}
@@ -168,7 +168,7 @@ public ValidateResult validate(Object value) {
168168
}
169169

170170
@Override
171-
public ValidateResult validate(long longValue) {
171+
protected ValidateResult validateInternal(long longValue) {
172172
if (minimum != Long.MIN_VALUE) {
173173
if (exclusiveMinimum ? longValue <= minimum : longValue < minimum) {
174174
return new ValidateResult(false, exclusiveMinimum ? "exclusiveMinimum not match, expect > %s, but %s" : "minimum not match, expect >= %s, but %s", minimum, longValue);
@@ -197,7 +197,7 @@ public ValidateResult validate(long longValue) {
197197
}
198198

199199
@Override
200-
public ValidateResult validate(Long value) {
200+
protected ValidateResult validateInternal(Long value) {
201201
if (value == null) {
202202
return typed ? FAIL_INPUT_NULL : SUCCESS;
203203
}
@@ -230,7 +230,7 @@ public ValidateResult validate(Long value) {
230230
}
231231

232232
@Override
233-
public ValidateResult validate(Integer value) {
233+
protected ValidateResult validateInternal(Integer value) {
234234
if (value == null) {
235235
return typed ? FAIL_INPUT_NULL : SUCCESS;
236236
}

core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ public abstract class JSONSchema {
3131

3232
final String title;
3333
final String description;
34+
final String customErrorMessage;
3435

3536
static final JSONReader.Context CONTEXT = JSONFactory.createReadContext();
3637

3738
JSONSchema(JSONObject input) {
3839
this.title = input.getString("title");
3940
this.description = input.getString("description");
41+
this.customErrorMessage = input.getString("error");
4042
}
4143

4244
JSONSchema(String title, String description) {
4345
this.title = title;
4446
this.description = description;
47+
this.customErrorMessage = null;
4548
}
4649

4750
void addResolveTask(UnresolvedReference.ResolveTask task){
@@ -686,7 +689,17 @@ public String getDescription() {
686689

687690
public abstract Type getType();
688691

689-
public abstract ValidateResult validate(Object value);
692+
protected abstract ValidateResult validateInternal(Object value);
693+
694+
public final ValidateResult validate(Object value) {
695+
ValidateResult result = validateInternal(value);
696+
697+
if (!result.isSuccess() && this.customErrorMessage != null) {
698+
return new ValidateResult(false, this.customErrorMessage);
699+
}
700+
701+
return result;
702+
}
690703

691704
public boolean isValid(Object value) {
692705
return validate(value)
@@ -728,28 +741,75 @@ public boolean isValid(Long value) {
728741
.isSuccess();
729742
}
730743

731-
public ValidateResult validate(long value) {
732-
return validate((Object) value);
744+
protected ValidateResult validateInternal(long value) {
745+
return validateInternal((Object) value);
746+
}
747+
748+
public final ValidateResult validate(long value) {
749+
ValidateResult result = validateInternal(value);
750+
if (!result.isSuccess() && customErrorMessage != null) {
751+
return new ValidateResult(false, customErrorMessage);
752+
}
753+
return result;
754+
}
755+
protected ValidateResult validateInternal(double value) {
756+
return validateInternal((Object) value);
757+
}
758+
759+
public final ValidateResult validate(double value) {
760+
ValidateResult result = validateInternal(value);
761+
if (!result.isSuccess() && customErrorMessage != null) {
762+
return new ValidateResult(false, customErrorMessage);
763+
}
764+
return result;
765+
}
766+
767+
protected ValidateResult validateInternal(Double value) {
768+
return validateInternal((Object) value);
733769
}
734770

735-
public ValidateResult validate(double value) {
736-
return validate((Object) value);
771+
public final ValidateResult validate(Double value) {
772+
ValidateResult result = validateInternal(value);
773+
if (!result.isSuccess() && customErrorMessage != null) {
774+
return new ValidateResult(false, customErrorMessage);
775+
}
776+
return result;
737777
}
738778

739-
public ValidateResult validate(Float value) {
740-
return validate((Object) value);
779+
protected ValidateResult validateInternal(Integer value) {
780+
return validateInternal((Object) value);
741781
}
742782

743-
public ValidateResult validate(Double value) {
744-
return validate((Object) value);
783+
public final ValidateResult validate(Integer value) {
784+
ValidateResult result = validateInternal(value);
785+
if (!result.isSuccess() && customErrorMessage != null) {
786+
return new ValidateResult(false, customErrorMessage);
787+
}
788+
return result;
745789
}
746790

747-
public ValidateResult validate(Integer value) {
748-
return validate((Object) value);
791+
protected ValidateResult validateInternal(Long value) {
792+
return validateInternal((Object) value);
749793
}
750794

751-
public ValidateResult validate(Long value) {
752-
return validate((Object) value);
795+
public final ValidateResult validate(Long value) {
796+
ValidateResult result = validateInternal(value);
797+
if (!result.isSuccess() && customErrorMessage != null) {
798+
return new ValidateResult(false, customErrorMessage);
799+
}
800+
return result;
801+
}
802+
803+
protected ValidateResult validateInternal(Float value) {
804+
return validateInternal((Object) value);
805+
}
806+
807+
public final ValidateResult validate(Float value) {
808+
ValidateResult result = validateInternal(value);
809+
if (!result.isSuccess() && customErrorMessage != null) {
810+
return new ValidateResult(false, customErrorMessage);
811+
}
812+
return result;
753813
}
754814

755815
public void assertValidate(Object value) {

core/src/main/java/com/alibaba/fastjson2/schema/Not.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Type getType() {
2727
}
2828

2929
@Override
30-
public ValidateResult validate(Object value) {
30+
protected ValidateResult validateInternal(Object value) {
3131
if (schema != null) {
3232
if (schema.validate(value).isSuccess()) {
3333
return FAIL_NOT;

core/src/main/java/com/alibaba/fastjson2/schema/NullSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Type getType() {
1414
}
1515

1616
@Override
17-
public ValidateResult validate(Object value) {
17+
protected ValidateResult validateInternal(Object value) {
1818
if (value == null) {
1919
return SUCCESS;
2020
}

0 commit comments

Comments
 (0)