From 2750a3df1834c1c6623aa8c653504c7bc12cefd6 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 29 Jan 2020 00:17:45 +0800 Subject: ... --- Timeline/Models/Http/User.cs | 9 +++------ Timeline/Models/User.cs | 19 +++++++++++++++++++ Timeline/Models/UserInfo.cs | 10 ---------- Timeline/Models/Validation/Validator.cs | 28 +++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 Timeline/Models/User.cs delete mode 100644 Timeline/Models/UserInfo.cs (limited to 'Timeline/Models') diff --git a/Timeline/Models/Http/User.cs b/Timeline/Models/Http/User.cs index 69bfacf2..b3812f48 100644 --- a/Timeline/Models/Http/User.cs +++ b/Timeline/Models/Http/User.cs @@ -1,14 +1,10 @@ +using System; using System.ComponentModel.DataAnnotations; using Timeline.Models.Validation; namespace Timeline.Models.Http { - public class User - { - public string Username { get; set; } = default!; - public bool Administrator { get; set; } - } - + [Obsolete("Remove this.")] public class UserPutRequest { [Required] @@ -17,6 +13,7 @@ namespace Timeline.Models.Http public bool? Administrator { get; set; } } + [Obsolete("Remove this.")] public class UserPatchRequest { public string? Password { get; set; } diff --git a/Timeline/Models/User.cs b/Timeline/Models/User.cs new file mode 100644 index 00000000..05395022 --- /dev/null +++ b/Timeline/Models/User.cs @@ -0,0 +1,19 @@ +using Timeline.Models.Validation; + +namespace Timeline.Models +{ + public class User + { + [Username] + public string? Username { get; set; } + public bool? Administrator { get; set; } + public string? Nickname { get; set; } + public string? AvatarUrl { get; set; } + + + #region secret + public string? Password { get; set; } + public long? Version { get; set; } + #endregion secret + } +} diff --git a/Timeline/Models/UserInfo.cs b/Timeline/Models/UserInfo.cs deleted file mode 100644 index eff47329..00000000 --- a/Timeline/Models/UserInfo.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Timeline.Models -{ - public class UserInfo - { - public long Id { get; set; } - public long Version { get; set; } - public string Username { get; set; } = default!; - public bool Administrator { get; set; } - } -} diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index a16f6f81..ead7dbef 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -20,24 +20,46 @@ namespace Timeline.Models.Validation (bool, string) Validate(object? value); } + public static class ValidatorExtensions + { + public static bool Validate(this IValidator validator, object? value, out string message) + { + if (validator == null) + throw new ArgumentNullException(nameof(validator)); + + var (r, m) = validator.Validate(value); + message = m; + return r; + } + } + /// /// Convenient base class for validator. /// /// The type of accepted value. /// /// Subclass should override to do the real validation. - /// This class will check the nullity and type of value. If value is null or not of type - /// it will return false and not call . + /// This class will check the nullity and type of value. + /// If value is null, it will pass or fail depending on . + /// If value is not null and not of type + /// it will fail and not call . + /// + /// is true by default. /// /// If you want some other behaviours, write the validator from scratch. /// public abstract class Validator : IValidator { + protected bool PermitNull { get; set; } = true; + public (bool, string) Validate(object? value) { if (value == null) { - return (false, ValidatorMessageNull); + if (PermitNull) + return (true, GetSuccessMessage()); + else + return (false, ValidatorMessageNull); } if (value is T v) -- cgit v1.2.3