0

Seem to be getting inconsistent returns in my model validation. The main issue that occurs is that whenever a date is invalid/malformed it will stop on all other validations besides date validations.

How is it possible that the first request does not return an error about the missing ParticipantUserId parameter, but the second request does? Is this intended behaviour?

Whilst debugging I've noticed that the missing parameter does not show up in the errors array, while logically it should be?

Examples:

Given the following model:

        [Required]
        [Range(1, int.MaxValue, ErrorMessage = "ParticipantUserId cannot be a negative number.")]
        public int? ParticipantUserId { get; set; }

        public DateTime? DateEnd { get; set; }

        public DateTime? DateStart { get; set; }

        [MaxLength(50)]
        public string ExternalReferenceKey { get; set; }

        [Required]
        public int? ScheduleId{get;set;}

Request #1 (Missing the required ParticipantUserId AND having an invalid date):

{
    "scheduleId": 1,
    "correspondenceNote": "DND",
    "dateStart": "2020-016T00:00:00",
    "dateEnd": "2020-07-16T00:00:00",
    "ExternalReferenceKey": null,
    "internalNoTe": "Note",
    "transferEnrollmentId": null
}

returns:

{
    "fields": [
        {
            "propertyName": "dateStart",
            "errorMessage": "Could not convert string to DateTime: 2020-016T00:00:00"
        }
    ],
    "errorMessage": "One or more validation errors occurred.",
    "statusCode": 400
}

Whereas

Request #2 (Missing the required ParticipantUserId, but with valid Dates):

{
    "scheduleId": 1,
    "correspondenceNote": "DND",
    "dateStart": "2020-07-16T00:00:00",
    "dateEnd": "2020-07-16T00:00:00",
    "ExternalReferenceKey": null,
    "internalNoTe": "Note",
    "transferEnrollmentId": null
}

returns:

{
    "fields": [
        {
            "propertyName": "ParticipantUserId",
            "errorMessage": "The field ParticipantUserId must be between 1 and 2147483647"
        }
    ],
    "errorMessage": "One or more validation errors occurred.",
    "statusCode": 400
}
jps
  • 20,041
  • 15
  • 75
  • 79
Paaz
  • 133
  • 7
  • Your response object does not look like a typical built-in badrequest response. Is that validation overridden somewhere in your application? Maybe a custom middleware? If Validator is called manually somewhere, make sure you provide the overload for `validateAllProperties` as `true`. – Silvermind Oct 12 '20 at 12:02
  • my guess would be that when binding the json to your model the modelbinding fails (invalid datetime) and no model constructed hence no validation will be performed. Try accepting a string instead of DateTime and use IValidatableObject for adding custom datetime parse checks – Jochem Van Hespen Oct 12 '20 at 12:04
  • umm, "ParticipantUserId cannot be a negative number." != "The field ParticipantUserId must be between 1 and 2147483647" – mcalex Oct 26 '21 at 08:54

0 Answers0