private static ValidationRuleSet BuildDefaultRuleSet() { ValidationRuleSet ruleSet = new ValidationRuleSet(); IEnumerable <Type> allTypes = typeof(ValidationRuleSet).Assembly.GetTypes().Where(t => t.IsClass && t != typeof(object)); Type validationRuleType = typeof(ValidationRule); foreach (Type type in allTypes) { if (!type.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Any()) { continue; } var properties = type.GetProperties(BindingFlags.Static | BindingFlags.Public); foreach (var property in properties) { if (validationRuleType.IsAssignableFrom(property.PropertyType)) { var propertyValue = property.GetValue(null); // static property ValidationRule rule = propertyValue as ValidationRule; if (rule != null) { ruleSet.Add(rule); } } } } return(ruleSet); }
/// <summary> /// Initializes a new instance of the <see cref="ValidationRuleSet"/> class. /// </summary> /// <param name="ruleSet">Rule set to be copied from.</param> public ValidationRuleSet(ValidationRuleSet ruleSet) { if (ruleSet == null) { return; } foreach (ValidationRule rule in ruleSet) { Add(rule); } }
/// <summary> /// Gets the default validation rule sets. /// </summary> /// <remarks> /// This is a method instead of a property to signal that a new default ruleset object is created /// per call. Making this a property may be misleading callers to think the returned rulesets from multiple calls /// are the same objects. /// </remarks> public static ValidationRuleSet GetDefaultRuleSet() { // Reflection can be an expensive operation, so we cache the default rule set that has already been built. if (_defaultRuleSet == null) { _defaultRuleSet = BuildDefaultRuleSet(); } // We create a new instance of ValidationRuleSet per call as a safeguard // against unintentional modification of the private _defaultRuleSet. return(new ValidationRuleSet(_defaultRuleSet)); }
private static ValidationRuleSet BuildDefaultRuleSet() { ValidationRuleSet ruleSet = new ValidationRuleSet(); Type validationRuleType = typeof(ValidationRule); IEnumerable <PropertyInfo> rules = typeof(ValidationRuleSet).Assembly.GetTypes() .Where(t => t.IsClass && t != typeof(object) && t.GetCustomAttributes(typeof(OpenApiRuleAttribute), false).Any()) .SelectMany(t2 => t2.GetProperties(BindingFlags.Static | BindingFlags.Public) .Where(p => validationRuleType.IsAssignableFrom(p.PropertyType))); foreach (var property in rules) { var propertyValue = property.GetValue(null); // static property ValidationRule rule = propertyValue as ValidationRule; if (rule != null) { ruleSet.Add(rule); } } return(ruleSet); }
/// <summary> /// Create a vistor that will validate an OpenAPIDocument /// </summary> /// <param name="ruleSet"></param> public OpenApiValidator(ValidationRuleSet ruleSet) { _ruleSet = ruleSet; }
/// <summary> /// Initializes the <see cref="ValidationContext"/> class. /// </summary> /// <param name="ruleSet"></param> public ValidationContext(ValidationRuleSet ruleSet) { RuleSet = ruleSet ?? throw Error.ArgumentNull(nameof(ruleSet)); }
/// <summary> /// Create a vistor that will validate an OpenAPIDocument /// </summary> /// <param name="ruleSet"></param> public OpenApiValidator(ValidationRuleSet ruleSet = null) { _ruleSet = ruleSet ?? ValidationRuleSet.GetDefaultRuleSet(); }
/// <summary> /// Create a vistor that will validate an OpenAPIDocument /// </summary> /// <param name="ruleSet"></param> public OpenApiValidator(ValidationRuleSet ruleSet = null) { _ruleSet = ruleSet ?? ValidationRuleSet.DefaultRuleSet; _context = new ValidationContext(_ruleSet); }