Is Dataannotation compatible with Json?

Asked

Viewed 96 times

-1

Hello! I would like to know if Dataannotation is compatible with Json, because I am validating a form and I would like to ask the question. I am using Angular on the front and returning my object in Json format, I would like to validate the return of that json (the information that the user typed in the fields) using Dataannotation

That’s my method of adding:

[HttpPost]
public async Task<string> Add([FromBody]Sindicate objSindicate)
{
    return JsonConvert.SerializeObject(await _service.Add(objSindicate));
}

This is my class using Dataannotation.

public class SindicateValidator
{
    [Key]

    public Guid SindicateId { get; set; }

    [Required(ErrorMessage = "Nome obrigatório!")]
    [MaxLength(128, ErrorMessage = "Máximo {0} caracteres")]
    [MinLength(2, ErrorMessage = "Minímo {0} caracteres")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Razão Social é obrigatório!")]
    [MaxLength(128, ErrorMessage = "Máximo {0} caracteres")]
    [MinLength(2, ErrorMessage = "Minímo {0} caracteres")]
    public string SocialReason { get; set; }

    [Required(ErrorMessage = "CNPJ é obrigatório!")]
    [DisplayFormat(DataFormatString = "{0:n2}", 
                    ApplyFormatInEditMode = true)]
    [MaxLength(14, ErrorMessage = "Máximo {0} caracteres")]
    public string CNPJ { get; set; }

    [Required(ErrorMessage = "Login é obrigatório!")]
    [MaxLength(128,ErrorMessage = "Máximo {0} caracteres")]
    public string Login { get; set; }

    [Required(ErrorMessage = "Senha é obrigatório!")]
    [MaxLength(8, ErrorMessage = "Máximo {0} caracteres")]
    public string Password { get; set; }

    [Required(ErrorMessage = "E-Mail é obrigatório!")]
    [MaxLength(128, ErrorMessage = "Máximo {0} caracteres")]
    [EmailAddress(ErrorMessage = "Preencha um E-mail válido!")]
    public string Email { get; set; }

    [ScaffoldColumn(false)]
    public DateTime DateCreated { get; set; }

    [ScaffoldColumn(false)]
    public bool Disable { get; set; }

    [ScaffoldColumn(false)]
    public DateTime? DateDisable { get; set; }

    [ScaffoldColumn(false)]
    public bool Deleted { get; set; }

    [ScaffoldColumn(false)]
    public DateTime? DateDeleted { get; set; }
}
  • Expose what you already have and explain your problem better.

1 answer

0


To Controller will validate the input independent of Content-Type used, but it is important that the input is with the ratings.

Another point, the API by default will serialize the return to JSON, then there is no need to serializar the object previously.

[HttpPost]
public async Task<IHttpActionResult> Add([FromBody]SindicateValidator objSindicate)
{
    if (!ModelState.IsValid)
        return BadRequest(ModelState);
    var response = await _service.Add(objSindicate);
    return Ok(response);
}
  • Tobias, I must do this validation in the following methods: Remove and Update?

  • remove normally only receives the id of the element to be removed, so there is no need, as the update needs to be cleared.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.