From 70808c29664dcb66acf6101d533007480b2d2293 Mon Sep 17 00:00:00 2001 From: jboinembalome Date: Thu, 11 Feb 2021 16:00:24 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20d'un=20attribut=20pour=20valide?= =?UTF-8?q?r=20une=20collection=20qui=20est=20pass=C3=A9e=20en=20param?= =?UTF-8?q?=C3=A8tre=20d'un=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit L'attribut CannotBeEmpty vérifie que la liste n'est pas nulle et que la liste contient au moins 1 élément. L'api renvoie une erreur 400 (BadRequest) dans le cas contraire. --- .../Attributes/CannotBeEmptyAttribute.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 EPAServeur/Attributes/CannotBeEmptyAttribute.cs diff --git a/EPAServeur/Attributes/CannotBeEmptyAttribute.cs b/EPAServeur/Attributes/CannotBeEmptyAttribute.cs new file mode 100644 index 0000000..b785ba1 --- /dev/null +++ b/EPAServeur/Attributes/CannotBeEmptyAttribute.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections; +using System.ComponentModel.DataAnnotations; + +namespace EPAServeur.Attributes +{ + /// + /// Specifies that a collection of a specified type must have at least one element. + /// + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] + public sealed class CannotBeEmptyAttribute : RequiredAttribute + { + public override bool IsValid(object value) + { + var list = value as IEnumerable; + return list != null && list.GetEnumerator().MoveNext(); + } + } +}