From 062dd56767bbc69444e4283151963fd8e309c6e6 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Wed, 14 Oct 2020 22:21:29 +0200 Subject: [PATCH] Add optional validation rule --- .../WebServer/Services/ValidationService.cs | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ChaosBot/WebServer/Services/ValidationService.cs b/ChaosBot/WebServer/Services/ValidationService.cs index c90f040..80b0599 100644 --- a/ChaosBot/WebServer/Services/ValidationService.cs +++ b/ChaosBot/WebServer/Services/ValidationService.cs @@ -29,6 +29,7 @@ namespace ChaosBot.WebServer.Services ValidationResult result = ruleType switch { + "optional" => CheckOptional(key, value), "required" => CheckRequired(key, value), "type" => CheckType(key, value, ruleParts), "min" => CheckMin(key, value, ruleParts), @@ -36,6 +37,9 @@ namespace ChaosBot.WebServer.Services _ => new ValidationResult.Unknown() }; + if (result.DoesForceSuccess()) + break; + if (result.GetError() != null) { errorBuilder.AppendLine($"[{result.GetKey()}] {result.GetError()}"); @@ -48,6 +52,13 @@ namespace ChaosBot.WebServer.Services return error.Length == 0; } + private ValidationResult CheckOptional(string key, dynamic value) + { + if (value == null) + return new ValidationResult.SuperSuccess(); + return new ValidationResult.Success(); + } + private ValidationResult CheckRequired(string key, dynamic value) { if (value != null) @@ -154,7 +165,15 @@ namespace ChaosBot.WebServer.Services _errorMessage = errorMessage; _key = key; } - + + internal class SuperSuccess : ValidationResult + { + public override bool DoesForceSuccess() + { + return true; + } + } + internal class Success : ValidationResult {} internal class Failure : ValidationResult @@ -172,5 +191,10 @@ namespace ChaosBot.WebServer.Services { return this._key; } + + public virtual bool DoesForceSuccess() + { + return false; + } } }