aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-29 00:17:45 +0800
committercrupest <crupest@outlook.com>2020-01-29 00:17:45 +0800
commit2750a3df1834c1c6623aa8c653504c7bc12cefd6 (patch)
tree480e4e0fa03f0736cfee97876603fdb87d3fd3bd /Timeline/Models
parent20347298c57e97287f586479fd9a52ba85565406 (diff)
downloadtimeline-2750a3df1834c1c6623aa8c653504c7bc12cefd6.tar.gz
timeline-2750a3df1834c1c6623aa8c653504c7bc12cefd6.tar.bz2
timeline-2750a3df1834c1c6623aa8c653504c7bc12cefd6.zip
...
Diffstat (limited to 'Timeline/Models')
-rw-r--r--Timeline/Models/Http/User.cs9
-rw-r--r--Timeline/Models/User.cs19
-rw-r--r--Timeline/Models/UserInfo.cs10
-rw-r--r--Timeline/Models/Validation/Validator.cs28
4 files changed, 47 insertions, 19 deletions
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;
+ }
+ }
+
/// <summary>
/// Convenient base class for validator.
/// </summary>
/// <typeparam name="T">The type of accepted value.</typeparam>
/// <remarks>
/// Subclass should override <see cref="DoValidate(T, out string)"/> to do the real validation.
- /// This class will check the nullity and type of value. If value is null or not of type <typeparamref name="T"/>
- /// it will return false and not call <see cref="DoValidate(T, out string)"/>.
+ /// This class will check the nullity and type of value.
+ /// If value is null, it will pass or fail depending on <see cref="PermitNull"/>.
+ /// If value is not null and not of type <typeparamref name="T"/>
+ /// it will fail and not call <see cref="DoValidate(T, out string)"/>.
+ ///
+ /// <see cref="PermitNull"/> is true by default.
///
/// If you want some other behaviours, write the validator from scratch.
/// </remarks>
public abstract class Validator<T> : 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)