Commit d155876f0957e0a3b84519f1530f8e444e9961c1
1 parent
343353b8
【代码优化】IoT: 物模型参数校验
Showing
6 changed files
with
48 additions
and
34 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/IotThingModelController.http
| ... | ... | @@ -174,7 +174,7 @@ GET {{baseUrl}}/iot/product-thing-model/get?id=67 |
| 174 | 174 | tenant-id: {{adminTenentId}} |
| 175 | 175 | Authorization: Bearer {{token}} |
| 176 | 176 | |
| 177 | -### 请求 /iot/product-thing-model/tsl-by-product-id 接口 => 成功 | |
| 178 | -GET {{baseUrl}}/iot/product-thing-model/tsl-by-product-id?productId=1001 | |
| 177 | +### 请求 /iot/product-thing-model/get-tsl 接口 => 成功 | |
| 178 | +GET {{baseUrl}}/iot/product-thing-model/get-tsl?productId=1001 | |
| 179 | 179 | tenant-id: {{adminTenentId}} |
| 180 | 180 | Authorization: Bearer {{token}} |
| 181 | 181 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/dataType/ThingModelArrayDataSpecs.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType; |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import jakarta.validation.Valid; | |
| 5 | +import jakarta.validation.constraints.NotEmpty; | |
| 6 | +import jakarta.validation.constraints.NotNull; | |
| 7 | +import jakarta.validation.constraints.Pattern; | |
| 4 | 8 | import lombok.Data; |
| 5 | 9 | import lombok.EqualsAndHashCode; |
| 6 | 10 | |
| ... | ... | @@ -16,18 +20,17 @@ import java.util.List; |
| 16 | 20 | @JsonIgnoreProperties({"dataType"}) // 忽略子类中的 dataType 字段,从而避免重复 |
| 17 | 21 | public class ThingModelArrayDataSpecs extends ThingModelDataSpecs { |
| 18 | 22 | |
| 19 | - /** | |
| 20 | - * 数组中的元素个数 | |
| 21 | - */ | |
| 23 | + @NotNull(message = "数组元素个数不能为空") | |
| 22 | 24 | private Integer size; |
| 23 | - /** | |
| 24 | - * 数组中的元素的数据类型。可选值:struct、int、float、double 或 text | |
| 25 | - */ | |
| 25 | + | |
| 26 | + @NotEmpty(message = "数组元素的数据类型不能为空") | |
| 27 | + @Pattern(regexp = "^(struct|int|float|double|text)$", message = "数组元素的数据类型必须为:struct、int、float、double 或 text") | |
| 26 | 28 | private String childDataType; |
| 27 | 29 | /** |
| 28 | 30 | * 数据类型(childDataType)为列表型 struct 的数据规范存储在 dataSpecsList 中 |
| 29 | 31 | * 此时 struct 取值范围为:int、float、double、text、date、enum、bool |
| 30 | 32 | */ |
| 33 | + @Valid | |
| 31 | 34 | private List<ThingModelDataSpecs> dataSpecsList; |
| 32 | 35 | |
| 33 | 36 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/dataType/ThingModelBoolOrEnumDataSpecs.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType; |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import jakarta.validation.constraints.NotEmpty; | |
| 5 | +import jakarta.validation.constraints.NotNull; | |
| 6 | +import jakarta.validation.constraints.Pattern; | |
| 4 | 7 | import lombok.Data; |
| 5 | 8 | import lombok.EqualsAndHashCode; |
| 6 | 9 | |
| ... | ... | @@ -16,16 +19,12 @@ import lombok.EqualsAndHashCode; |
| 16 | 19 | @JsonIgnoreProperties({"dataType"}) // 忽略子类中的 dataType 字段,从而避免重复 |
| 17 | 20 | public class ThingModelBoolOrEnumDataSpecs extends ThingModelDataSpecs { |
| 18 | 21 | |
| 19 | - // TODO @puhui999:要不写下参数校验?这样,注释可以简洁一点 | |
| 20 | - /** | |
| 21 | - * 枚举项的名称。 | |
| 22 | - * 可包含中文、大小写英文字母、数字、下划线(_)和短划线(-) | |
| 23 | - * 必须以中文、英文字母或数字开头,长度不超过 20 个字符 | |
| 24 | - */ | |
| 22 | + @NotEmpty(message = "枚举项的名称不能为空") | |
| 23 | + @Pattern(regexp = "^[\\u4e00-\\u9fa5a-zA-Z0-9][\\u4e00-\\u9fa5a-zA-Z0-9_-]{0,19}$", | |
| 24 | + message = "枚举项的名称只能包含中文、大小写英文字母、数字、下划线和短划线,必须以中文、英文字母或数字开头,长度不超过 20 个字符") | |
| 25 | 25 | private String name; |
| 26 | - /** | |
| 27 | - * 枚举值。 | |
| 28 | - */ | |
| 26 | + | |
| 27 | + @NotNull(message = "枚举值不能为空") | |
| 29 | 28 | private Integer value; |
| 30 | 29 | |
| 31 | 30 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/dataType/ThingModelDateOrTextDataSpecs.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType; |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import jakarta.validation.constraints.Max; | |
| 4 | 5 | import lombok.Data; |
| 5 | 6 | import lombok.EqualsAndHashCode; |
| 6 | 7 | |
| ... | ... | @@ -20,6 +21,7 @@ public class ThingModelDateOrTextDataSpecs extends ThingModelDataSpecs { |
| 20 | 21 | * 数据长度,单位为字节。取值不能超过 2048。 |
| 21 | 22 | * 当 dataType 为 text 时,需传入该参数。 |
| 22 | 23 | */ |
| 24 | + @Max(value = 2048, message = "数据长度不能超过 2048") | |
| 23 | 25 | private Integer length; |
| 24 | 26 | /** |
| 25 | 27 | * 默认值,可选参数,用于存储默认值。 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/dataType/ThingModelNumericDataSpec.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType; |
| 2 | 2 | |
| 3 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import jakarta.validation.constraints.NotEmpty; | |
| 5 | +import jakarta.validation.constraints.Pattern; | |
| 4 | 6 | import lombok.Data; |
| 5 | 7 | import lombok.EqualsAndHashCode; |
| 6 | 8 | |
| ... | ... | @@ -18,18 +20,21 @@ public class ThingModelNumericDataSpec extends ThingModelDataSpecs { |
| 18 | 20 | |
| 19 | 21 | /** |
| 20 | 22 | * 最大值,需转为字符串类型。值必须与 dataType 类型一致。 |
| 21 | - * 例如,当 dataType 为 int 时,取值为 "200",而不是 200。 | |
| 22 | 23 | */ |
| 24 | + @NotEmpty(message = "最大值不能为空") | |
| 25 | + @Pattern(regexp = "^-?\\d+(\\.\\d+)?$", message = "最大值必须为数值类型") | |
| 23 | 26 | private String max; |
| 24 | 27 | /** |
| 25 | 28 | * 最小值,需转为字符串类型。值必须与 dataType 类型一致。 |
| 26 | - * 例如,当 dataType 为 int 时,取值为 "0",而不是 0。 | |
| 27 | 29 | */ |
| 30 | + @NotEmpty(message = "最小值不能为空") | |
| 31 | + @Pattern(regexp = "^-?\\d+(\\.\\d+)?$", message = "最小值必须为数值类型") | |
| 28 | 32 | private String min; |
| 29 | 33 | /** |
| 30 | 34 | * 步长,需转为字符串类型。值必须与 dataType 类型一致。 |
| 31 | - * 例如,当 dataType 为 int 时,取值为 "10",而不是 10。 | |
| 32 | 35 | */ |
| 36 | + @NotEmpty(message = "步长不能为空") | |
| 37 | + @Pattern(regexp = "^-?\\d+(\\.\\d+)?$", message = "步长必须为数值类型") | |
| 33 | 38 | private String step; |
| 34 | 39 | /** |
| 35 | 40 | * 精度。当 dataType 为 float 或 double 时可选传入。 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/dataType/ThingModelStructDataSpecs.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.validation.InEnum; | |
| 3 | 4 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelAccessModeEnum; |
| 4 | 5 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 6 | +import jakarta.validation.Valid; | |
| 7 | +import jakarta.validation.constraints.NotEmpty; | |
| 8 | +import jakarta.validation.constraints.Pattern; | |
| 5 | 9 | import lombok.Data; |
| 6 | 10 | import lombok.EqualsAndHashCode; |
| 7 | 11 | |
| ... | ... | @@ -17,35 +21,36 @@ import java.util.List; |
| 17 | 21 | @JsonIgnoreProperties({"dataType"}) // 忽略子类中的 dataType 字段,从而避免重复 |
| 18 | 22 | public class ThingModelStructDataSpecs extends ThingModelDataSpecs { |
| 19 | 23 | |
| 20 | - /** | |
| 21 | - * 属性标识符 | |
| 22 | - */ | |
| 24 | + @NotEmpty(message = "属性标识符不能为空") | |
| 25 | + @Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9_]{0,31}$", message = "属性标识符只能由字母、数字和下划线组成,必须以字母开头,长度不超过 32 个字符") | |
| 23 | 26 | private String identifier; |
| 24 | - /** | |
| 25 | - * 属性名称 | |
| 26 | - */ | |
| 27 | + | |
| 28 | + @NotEmpty(message = "属性名称不能为空") | |
| 27 | 29 | private String name; |
| 28 | - /** | |
| 29 | - * 云端可以对该属性进行的操作类型 | |
| 30 | - * | |
| 31 | - * 枚举 {@link IotThingModelAccessModeEnum} | |
| 32 | - */ | |
| 30 | + | |
| 31 | + @NotEmpty(message = "操作类型不能为空") | |
| 32 | + @InEnum(IotThingModelAccessModeEnum.class) | |
| 33 | 33 | private String accessMode; |
| 34 | + | |
| 34 | 35 | /** |
| 35 | 36 | * 是否是标准品类的必选服务 |
| 36 | 37 | */ |
| 37 | 38 | private Boolean required; |
| 38 | - /** | |
| 39 | - * struct 数据的数据类型 | |
| 40 | - */ | |
| 39 | + | |
| 40 | + @NotEmpty(message = "数据类型不能为空") | |
| 41 | + @Pattern(regexp = "^(int|float|double|text|date|enum|bool)$", message = "数据类型必须为:int、float、double、text、date、enum、bool") | |
| 41 | 42 | private String childDataType; |
| 43 | + | |
| 42 | 44 | /** |
| 43 | 45 | * 数据类型(dataType)为非列表型(int、float、double、text、date、array)的数据规范存储在 dataSpecs 中 |
| 44 | 46 | */ |
| 47 | + @Valid | |
| 45 | 48 | private ThingModelDataSpecs dataSpecs; |
| 49 | + | |
| 46 | 50 | /** |
| 47 | 51 | * 数据类型(dataType)为列表型(enum、bool、struct)的数据规范存储在 dataSpecsList 中 |
| 48 | 52 | */ |
| 53 | + @Valid | |
| 49 | 54 | private List<ThingModelDataSpecs> dataSpecsList; |
| 50 | 55 | |
| 51 | 56 | } | ... | ... |