Add optional validation rule
This commit is contained in:
parent
da7ff701ef
commit
062dd56767
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user