Created
July 5, 2021 22:52
-
-
Save mrezekiel/ee1b793122583738abb5be43f07b2c69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OvfgNx.Data; | |
using OvfgNx.Models; | |
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.Reflection; | |
namespace OvfgNx.Validators | |
{ | |
/// <summary> | |
/// This vaidator checks that the job contains the serial that the user has entered | |
/// </summary> | |
public class DiscardSerial : ValidationAttribute | |
{ | |
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
{ | |
// get the serial and be sure it is an int | |
if (value.GetType() != typeof(int)) | |
{ | |
return new ValidationResult("This validator can only be used on an integer"); | |
} | |
int serial = (int)value; | |
OvfgNxContext context = (OvfgNxContext)validationContext.GetService(typeof(OvfgNxContext)); | |
var model = validationContext.ObjectInstance; | |
Type type = model.GetType(); | |
// Check JobId property exists | |
PropertyInfo prop = type.GetProperty("JobId"); | |
if (prop == null) | |
{ | |
return new ValidationResult("This validator can only be used on models containing a JobId"); | |
} | |
// Get the JobId | |
int jobId = (int)prop.GetValue(model); | |
// Get the job | |
Job job = context.Job.Find(jobId); | |
// Check the job exists | |
if (job == null) | |
{ | |
return new ValidationResult($"Cannot find job with the ID of: {job}"); | |
} | |
// Check the value falls within sn1 and sn2 in the job | |
if ((serial >= job.SerialNumber1) && (serial <= job.SerialNumber2)) | |
{ | |
return ValidationResult.Success; | |
} | |
return new ValidationResult("Serial Number is outside of Job Serial range"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OvfgNx.Data; | |
using OvfgNx.Models; | |
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.Reflection; | |
namespace OvfgNx.Validators | |
{ | |
/// <summary> | |
/// This vaidator checks that the job contains the serial that the user has entered | |
/// </summary> | |
public class DiscardSerial : ValidationAttribute | |
{ | |
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
{ | |
// get the serial and be sure it is an int | |
if (value.GetType() != typeof(int)) | |
{ | |
return new ValidationResult("This validator can only be used on an integer"); | |
} | |
int serial = (int)value; | |
OvfgNxContext context = (OvfgNxContext)validationContext.GetService(typeof(OvfgNxContext)); | |
var model = validationContext.ObjectInstance; | |
Type type = model.GetType(); | |
// Check JobId property exists | |
PropertyInfo prop = type.GetProperty("JobId"); | |
if (prop == null) | |
{ | |
return new ValidationResult("This validator can only be used on models containing a JobId"); | |
} | |
// Get the JobId | |
int jobId = (int)prop.GetValue(model); | |
// Get the job | |
Job job = context.Job.Find(jobId); | |
// Check the job exists | |
if (job == null) | |
{ | |
return new ValidationResult($"Cannot find job with the ID of: {job}"); | |
} | |
// Check the value falls within sn1 and sn2 in the job | |
if ((serial >= job.SerialNumber1) && (serial <= job.SerialNumber2)) | |
{ | |
return ValidationResult.Success; | |
} | |
return new ValidationResult("Serial Number is outside of Job Serial range"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment