验证约束,DTO中的可选字段?

时间:2018-11-27 12:31:37

标签: java javax.validation

我有一个DTO,其中包含几个字段,用户名,密码和电子邮件,它们都是字符串。

我还有一个想要作为可选项的列表,这意味着它不必从表单中传递,也可以保留为空白。

这应该在前端有效:

{
"username":"user",
"email":"email@email.com",
"password": "123",
"hobbies":[ "Weightlifting", "Dancing"]
} 

这也应该:

{
"username":"user",
"email":"email@email.com",
"password": "123"
} 

javax.validation.constraints中是否有@OptionalField批注或类似内容?

还是我唯一的选择是两个单独的DTO?

1 个答案:

答案 0 :(得分:0)

在这种情况下,解决方案是将该字段设为可选。例如public class LTButton : Android.Support.V7.Widget.AppCompatButton { Context context; public LTButton(Context pContext, IAttributeSet attrs) : base(pContext, attrs) { context = pContext; Elevation = 0; } protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); Init(); } void Init() { // Adding a left drawable Drawable img = ContextCompat.GetDrawable(context, Resource.Drawable.chevron_right); img.SetBounds(0, 0, 70, 70); SetCompoundDrawables(img, null, null, null); TextSize = 16; // The shape with background color and rounded corners var shape = new GradientDrawable(); // This is a full rounded button shape.SetCornerRadius(Height / 2); shape.SetColor(ContextCompat.GetColor(context, Resource.Color.LTGreen)); Background = shape; // Foreground => To have a ripple effect int[] attrs = { Android.Resource.Attribute.SelectableItemBackground }; TypedArray ta = context.ObtainStyledAttributes(attrs); Drawable selectableDrawable = ta.GetDrawable(0); ta.Recycle(); Foreground = selectableDrawable; } }