vendredi 2 septembre 2016

Return object where validation failed on DataMember

I have a DataContract class that looks something like

[DataContract]
public class Foo
{
    [DataMember(Name = "id)]
    [Required]
    public string Id { get; set; }
}

I have a class that represents error responses as

[DataContract]
public class ErrorResponse
{
    [DataMember(Name = "code")]
    [Required]
    public int Code { get; set; }

    [DataMember(Name = "message")]
    [Required]
    public string Message { get; set; }
}

What I want to do is return an instance of the ErrorModel class if the validation on id fails (in this case, if it was not specified.

Is there some attribute I can use to achieve this? Something like

[Required(new ErrorResponse { Code = 1, Message = "id field is required })]

Aucun commentaire:

Enregistrer un commentaire