Skip to content

Commit 898e428

Browse files
committed
Server:Structure新增TYPE:{ key:type }校验值类型
1 parent e3ddeb6 commit 898e428

2 files changed

Lines changed: 86 additions & 8 deletions

File tree

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Operation.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ public enum Operation {
3030
*/
3131
NECESSARY,
3232

33+
/**
34+
* 验证是否符合预设的类型:
35+
* Boolean, Long, Double, String, Object, Array
36+
*/
37+
TYPE,
3338
/**
3439
* 验证是否符合预设的条件
3540
*/
3641
VERIFY,
37-
/**TODO
38-
* 验证是否符合预设的类型
39-
*/
40-
TYPE,
4142
/**
4243
* 验证是否不存在,除了本身的记录
4344
*/

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import static zuo.biao.apijson.server.Operation.REPLACE;
2525
import static zuo.biao.apijson.server.Operation.UNIQUE;
2626
import static zuo.biao.apijson.server.Operation.VERIFY;
27+
import static zuo.biao.apijson.server.Operation.TYPE;
2728

2829
import java.util.ArrayList;
2930
import java.util.Arrays;
31+
import java.util.Collection;
3032
import java.util.HashSet;
3133
import java.util.LinkedHashSet;
3234
import java.util.List;
35+
import java.util.Map;
3336
import java.util.Map.Entry;
3437
import java.util.Set;
3538
import java.util.regex.Pattern;
@@ -211,6 +214,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
211214

212215

213216
//获取配置<<<<<<<<<<<<<<<<<<<<<<<<<<<<
217+
JSONObject type = target.getJSONObject(TYPE.name());
214218
JSONObject verify = target.getJSONObject(VERIFY.name());
215219
JSONObject add = target.getJSONObject(ADD.name());
216220
JSONObject put = target.getJSONObject(PUT.name());
@@ -222,6 +226,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
222226
String disallow = StringUtil.getNoBlankString(target.getString(DISALLOW.name()));
223227

224228
//不还原,传进来的target不应该是原来的
229+
target.remove(TYPE.name());
225230
target.remove(VERIFY.name());
226231
target.remove(ADD.name());
227232
target.remove(PUT.name());
@@ -341,6 +346,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
341346

342347
//校验与修改Request<<<<<<<<<<<<<<<<<
343348
//在tableKeySet校验后操作,避免 导致put/add进去的Table 被当成原Request的内容
349+
real = operate(TYPE, type, real, creator);
344350
real = operate(VERIFY, verify, real, creator);
345351
real = operate(ADD, add, real, creator);
346352
real = operate(PUT, put, real, creator);
@@ -392,16 +398,22 @@ private static JSONObject operate(Operation opt, JSONObject targetChild, JSONObj
392398
}
393399
tv = e.getValue();
394400

395-
if (opt == VERIFY) {
401+
if (opt == TYPE) {
402+
type(tk, tv, real);
403+
}
404+
else if (opt == VERIFY) {
396405
verify(tk, tv, real, creator);
397-
} else if (opt == PUT) {
406+
}
407+
else if (opt == PUT) {
398408
real.put(tk, tv);
399-
} else {
409+
}
410+
else {
400411
if (real.containsKey(tk)) {
401412
if (opt == REPLACE) {
402413
real.put(tk, tv);
403414
}
404-
} else {
415+
}
416+
else {
405417
if (opt == ADD) {
406418
real.put(tk, tv);
407419
}
@@ -413,6 +425,71 @@ private static JSONObject operate(Operation opt, JSONObject targetChild, JSONObj
413425
}
414426

415427

428+
/**验证值类型
429+
* @param tk
430+
* @param tv
431+
* @param real
432+
* @throws Exception
433+
*/
434+
private static void type(@NotNull String tk, Object tv, @NotNull JSONObject real) throws Exception {
435+
if (tv == null) {
436+
return;
437+
}
438+
if (tv instanceof String == false) {
439+
throw new UnsupportedDataTypeException("服务器内部错误," + tk + ":value 的value不合法!"
440+
+ "Request表校验规则中 TYPE:{ key:value } 中的value只能是String类型!");
441+
}
442+
String t = (String) tv;
443+
Object rv = real.get(tk);
444+
if (rv == null) {
445+
return;
446+
}
447+
448+
switch (t) {
449+
case "Boolean":
450+
//Boolean.parseBoolean(real.getString(tk)); 只会判断null和true
451+
if (rv instanceof Boolean == false) { //JSONObject.getBoolean 可转换Number类型
452+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Boolean !");
453+
}
454+
break;
455+
case "Long":
456+
try {
457+
Long.parseLong(real.getString(tk)); //1.23会转换为1 real.getLong(tk);
458+
} catch (Exception e) {
459+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Long !");
460+
}
461+
break;
462+
case "Double":
463+
try {
464+
Double.parseDouble(rv.toString());
465+
} catch (Exception e) {
466+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 Double !");
467+
}
468+
break;
469+
case "String":
470+
if (rv instanceof String == false) { //JSONObject.getString 可转换任何类型
471+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 String !");
472+
}
473+
break;
474+
case "Object":
475+
if (rv instanceof Map == false) { //JSONObject.getJSONObject 可转换String类型
476+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 {Object} !");
477+
}
478+
break;
479+
case "Array":
480+
if (rv instanceof Collection == false) { //JSONObject.getJSONArray 可转换String类型
481+
throw new UnsupportedDataTypeException(tk + ":value 的value不合法!类型必须是 [Array] !");
482+
}
483+
break;
484+
default:
485+
throw new UnsupportedDataTypeException("服务器内部错误,类型 " + t + " 不合法!Request表校验规则中"
486+
+ " TYPE:{ key:value } 中的value类型必须是 [Boolean, Long, Double, String, Object, Array] 中的一个!");
487+
}
488+
}
489+
490+
491+
492+
416493
/**验证值
417494
* @param tk
418495
* @param tv

0 commit comments

Comments
 (0)