Repository navigation
Expand file tree
/
Copy pathValidationRule.cs
More file actions
70 lines (60 loc) · 2.04 KB
/
Copy pathValidationRule.cs
File metadata and controls
70 lines (60 loc) · 2.04 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
67
68
69
70
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace LEGO.AsyncAPI.Validations
{
using System;
using LEGO.AsyncAPI.Models.Interfaces;
/// <summary>
/// Class containing validation rule logic.
/// </summary>
public abstract class ValidationRule
{
/// <summary>
/// Element Type.
/// </summary>
internal abstract Type ElementType { get; }
/// <summary>
/// Validate the object.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="item">The object item.</param>
internal abstract void Evaluate(IValidationContext context, object item);
}
/// <summary>
/// Class containing validation rule logic for <see cref="IAsyncApiElement"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ValidationRule<T> : ValidationRule where T : IAsyncApiElement
{
private readonly Action<IValidationContext, T> validate;
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRule"/> class.
/// </summary>
/// <param name="validate">Action to perform the validation.</param>
public ValidationRule(Action<IValidationContext, T> validate)
{
this.validate = validate ?? throw Error.ArgumentNull(nameof(validate));
}
internal override Type ElementType
{
get { return typeof(T); }
}
internal override void Evaluate(IValidationContext context, object item)
{
if (context == null)
{
throw Error.ArgumentNull(nameof(context));
}
if (item == null)
{
return;
}
if (!(item is T))
{
throw Error.Argument(string.Format("Input type must be of type {0}", typeof(T).FullName));
}
T typedItem = (T)item;
this.validate(context, typedItem);
}
}
}