-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarModel.cs
More file actions
50 lines (43 loc) · 1.75 KB
/
CarModel.cs
File metadata and controls
50 lines (43 loc) · 1.75 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
namespace CarServiceAPI.Models;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
public class CarModel
{
public required string Id { get; set; } // UUIDs are strings, not integers
[MakeValidation(ErrorMessage = "Make must be one of these specific car brands: Toyota, Ford, Chevrolet, Honda, Mercedes-Benz, BMW, Audi, Tesla, Nissan, Hyundai")]
public required string Make { get; set; }
[JsonProperty("runs_on")]
[RunsOnValidation(ErrorMessage = "Value for RunsOn must be 'petrol', 'diesel', or 'electric'.")]
public required string RunsOn { get; set; } // This property holds the fuel type (petrol, diesel, electric)
}
public class MakeValidation : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var allowedMakes = new string[] { "Toyota", "Ford", "Chevrolet", "Honda", "Mercedes-Benz",
"BMW", "Audi", "Tesla", "Nissan", "Hyundai" };
if (value is string makeValue && allowedMakes.Any(m => m.Equals(makeValue, StringComparison.OrdinalIgnoreCase)))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(ErrorMessage);
}
}
}
public class RunsOnValidation : ValidationAttribute
{
public string[] AllowedValues = new string[] { "petrol", "diesel", "electric" };
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value is string runsOnValue && AllowedValues.Contains(runsOnValue.ToLower()))
{
return ValidationResult.Success;
}
else
{
return new ValidationResult(ErrorMessage);
}
}
}