-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathValidateScripts.cs
More file actions
66 lines (52 loc) · 3.42 KB
/
Copy pathValidateScripts.cs
File metadata and controls
66 lines (52 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using ServiceStack.Configuration;
using ServiceStack.FluentValidation.Validators;
using ServiceStack.Script;
namespace ServiceStack
{
public class ValidateScripts : ScriptMethods
{
public static HashSet<string> RequiredValidators { get; } = new() {
nameof(NotNull),
nameof(NotEmpty),
};
public static ValidateScripts Instance = new();
//Note: Can't use singleton validators in-case ErrorCode/Messages are customized
public IPropertyValidator Null() => new NullValidator();
public IPropertyValidator Empty() => new EmptyValidator(null);
public IPropertyValidator Empty(object defaultValue) => new EmptyValidator(defaultValue);
public IPropertyValidator Equal(object value) => new EqualValidator(value);
public IPropertyValidator NotNull() => new NotNullValidator();
public IPropertyValidator NotEmpty() => new NotEmptyValidator(null);
public IPropertyValidator NotEmpty(object defaultValue) => new NotEmptyValidator(defaultValue);
public IPropertyValidator NotEqual(object value) => new NotEqualValidator(value);
public IPropertyValidator CreditCard() => new CreditCardValidator();
public IPropertyValidator Email() => new AspNetCoreCompatibleEmailValidator();
public IPropertyValidator Length(int min, int max) => new LengthValidator(min, max);
public IPropertyValidator ExactLength(int length) => new ExactLengthValidator(length);
public IPropertyValidator MaximumLength(int max) => new MaximumLengthValidator(max);
public IPropertyValidator MinimumLength(int min) => new MinimumLengthValidator(min);
public IPropertyValidator InclusiveBetween(IComparable from, IComparable to) =>
new InclusiveBetweenValidator(from, to);
public IPropertyValidator ExclusiveBetween(IComparable from, IComparable to) =>
new ExclusiveBetweenValidator(from, to);
public IPropertyValidator LessThan(int value) => new LessThanValidator(value);
public IPropertyValidator LessThanOrEqual(int value) => new LessThanOrEqualValidator(value);
public IPropertyValidator GreaterThan(int value) => new GreaterThanValidator(value);
public IPropertyValidator GreaterThanOrEqual(int value) => new GreaterThanOrEqualValidator(value);
public IPropertyValidator ScalePrecision(int scale, int precision) =>
new ScalePrecisionValidator(scale, precision);
public IPropertyValidator RegularExpression(string regex) =>
new RegularExpressionValidator(regex, RegexOptions.Compiled);
public IPropertyValidator Enum(Type enumType) => new EnumValidator(enumType);
public ITypeValidator IsAuthenticated() => new IsAuthenticatedValidator();
public ITypeValidator IsAuthenticated(string provider) => new IsAuthenticatedValidator(provider);
public ITypeValidator HasRole(string role) => new HasRolesValidator(role);
public ITypeValidator HasRoles(string[] roles) => new HasRolesValidator(roles);
public ITypeValidator HasPermission(string permission) => new HasPermissionsValidator(permission);
public ITypeValidator HasPermissions(string[] permission) => new HasPermissionsValidator(permission);
public ITypeValidator IsAdmin() => new HasRolesValidator(RoleNames.Admin);
}
}