// Verify and Retrieve the Boolean Attribute. If it is not // a valid value then log an error and set the value to a given default. internal void VerifyAndGetBooleanAttribute( ExceptionAction action, bool defaultValue, out bool newValue) { if (Reader.Value == "true") { newValue = true; } else { if (Reader.Value == "false") { newValue = false; } else { // Unrecognized value newValue = defaultValue; ConfigurationErrorsException ex = new ConfigurationErrorsException( string.Format(SR.Config_invalid_boolean_attribute, Reader.Name), this); SchemaErrors.AddError(ex, action); } } }
internal void AddErrorUnrecognizedElement(ExceptionAction action) { ConfigurationErrorsException ex = new ConfigurationErrorsException( SR.Config_base_unrecognized_element, this); SchemaErrors.AddError(ex, action); }
internal void AddErrorRequiredAttribute(string attrib, ExceptionAction action) { ConfigurationErrorsException ex = new ConfigurationErrorsException( SR.Format(SR.Config_missing_required_attribute, attrib, Reader.Name), this); SchemaErrors.AddError(ex, action); }
internal void AddErrorReservedAttribute(ExceptionAction action) { ConfigurationErrorsException ex = new ConfigurationErrorsException( string.Format(SR.Config_reserved_attribute, Reader.Name), this); SchemaErrors.AddError(ex, action); }
internal void AddErrorUnrecognizedAttribute(ExceptionAction action) { ConfigurationErrorsException ex = new ConfigurationErrorsException( SR.Format(SR.Config_base_unrecognized_attribute, Reader.Name), this); SchemaErrors.AddError(ex, action); }
public void Validate() { XmlNamespaceManager nsmgr = new BindsNamespaceManager(); XmlReaderSettings settings = new XmlReaderSettings(); var a = Assembly.GetEntryAssembly(); settings.Schemas.Add(null, XmlReader.Create(a.GetManifestResourceStream(SchemaResource))); settings.Schemas.Add(null, XmlReader.Create(a.GetManifestResourceStream(ProcessingSchemaResource))); settings.ValidationType = ValidationType.Schema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None); if (nsmgr.HasNamespace(String.Empty)) { Console.WriteLine(nsmgr.DefaultNamespace); } XmlReader reader = XmlReader.Create( inputUri: Path, settings: settings, inputContext: context ); SchemaErrors.Clear(); while (reader.Read()) { if (reader.NodeType is XmlNodeType.Element) { var si = reader.SchemaInfo; var se = si.SchemaElement; if (se == null) { // The file probably stopped making sense, keep reading to emit errors and maybe get lucky. continue; } if (se.SchemaTypeName.Namespace == BindsSchemaNamespace) { switch (se.SchemaTypeName.Name) { case "bindsContainer": EvaluateRoot(reader); break; } } } } }
public bool Check(IModel model) { ILogger log = XbimLogging.CreateLogger <IfcValidator>(); // check for parser exceptions var v = new Validator { ValidateLevel = ValidationFlags.All, CreateEntityHierarchy = true }; SchemaErrors = v.Validate(model.Instances).ToList(); TemplateErrors = CheckPropertyTemplateTypesAndUnits(model).ToList(); PropertyErrors = CheckPropertyUnits(model).ToList(); foreach (var err in SchemaErrors .Concat(TemplateErrors) .Concat(PropertyErrors)) { var identity = err.Item.GetType().Name; if (err.Item is IPersistEntity entity) { identity = $"#{entity.EntityLabel}={entity.ExpressType.ExpressName}"; } var msg = new StringBuilder(); msg.AppendLine($"{identity} is invalid."); var details = new Stack <ValidationResult>(err.Details); while (details.Any()) { var detail = details.Pop(); foreach (var d in detail.Details) { details.Push(d); } var report = detail.Message; if (string.IsNullOrWhiteSpace(report)) { report = detail.Report(); } msg.AppendLine(" " + report); if (detail.IssueType == ValidationFlags.EntityWhereClauses || detail.IssueType == ValidationFlags.TypeWhereClauses) { var source = detail.IssueSource.Split('.')[0].ToLower(); msg.AppendLine($"http://www.buildingsmart-tech.org/ifc/IFC4/Add2/html/link/{source}.htm"); } } log.LogError(msg.ToString()); } return(!SchemaErrors.Any() && !TemplateErrors.Any() && !PropertyErrors.Any()); }
// Add an error if the node type is not permitted by the configuration schema. internal void VerifyIgnorableNodeType(ExceptionAction action) { XmlNodeType nodeType = Reader.NodeType; if ((nodeType != XmlNodeType.Comment) && (nodeType != XmlNodeType.EndElement)) { ConfigurationException ex = new ConfigurationErrorsException( SR.Config_base_unrecognized_element, this); SchemaErrors.AddError(ex, action); } }
private bool CheckError(IEdmTypeReference edmTypeReference) { var edmType = edmTypeReference.Definition; if (!edmType.Errors().Any()) { return(false); } var error = edmType.Errors().Select(x => $"Location: {x.ErrorLocation.ToString()}, {x.ErrorMessage }").FirstOrDefault(); _logger.Trace($"edmTypeReference Error: {error.Dump()}"); _logger.Warn($"Invalid Type Reference: {edmType}"); SchemaErrors.Add($"Invalid Type Reference: {edmType}"); return(true); }
internal void VerifyAndGetNonEmptyStringAttribute(ExceptionAction action, out string newValue) { if (!string.IsNullOrEmpty(Reader.Value)) { newValue = Reader.Value; } else { newValue = null; ConfigurationException ex = new ConfigurationErrorsException( string.Format(SR.Empty_attribute, Reader.Name), this); SchemaErrors.AddError(ex, action); } }
/// <summary> /// Verify and Retrieve the Boolean Attribute. If it is not /// a valid value then log an error and set the value to a given default. /// </summary> internal void VerifyAndGetBooleanAttribute( ExceptionAction action, bool defaultValue, out bool newValue) { switch (Reader.Value) { case "true": newValue = true; break; case "false": newValue = false; break; default: newValue = defaultValue; SchemaErrors.AddError( new ConfigurationErrorsException(string.Format(SR.Config_invalid_boolean_attribute, Reader.Name), this), action); break; } }
private static void ProcessSchema(SchemaRules schema, ProjectRootElement project) { if (schema == null) { throw new ArgumentNullException("schema"); } if (project == null) { throw new ArgumentNullException("project"); } foreach (var rules in schema.TypeRulesSet) { var wrTemplate = new WhereError(rules, schema.Schema); ProcessTemplate(wrTemplate, project); var rTemplate = new SchemaRule(rules, schema.Schema); ProcessTemplate(rTemplate, project); } var schErrsTmpl = new SchemaErrors(schema); ProcessTemplate(schErrsTmpl, project); }
public static bool IsValid(JsonObj data, JsonObj schema, JsonObj node = null) { if (node == null) { SchemaError = SchemaErrors.None; if (!schema["type"].Equals("object")) { throw new JsonException("Can only validate objects."); } return(IsValid(data, schema, schema)); } var properties = node["properties"] as JsonObj; if (node.ContainsKey("required")) { foreach (var requirement in (node["required"] as List <object>).Select(i => i.ToString())) { if (!data.ContainsKey(requirement)) { SchemaError = SchemaErrors.MissingRequirement; return(false); } } } foreach (var property in properties) { var key = property.Key; var value = property.Value as JsonObj; var type = value["type"] as string; if (data.ContainsKey(key)) { var item = data[key]; if (!typesMatch(type, item)) { SchemaError = SchemaErrors.TypeMismatch; return(false); } if (type == "object") { if (!IsValid(item as JsonObj, schema, value)) { return(false); } } if (type == "array") { var array = (IEnumerable <object>)item; if (value.ContainsKey("items")) { var items = value["items"] as JsonObj; var itemType = items["type"] as string; foreach (var thing in array) { if (!typesMatch(itemType, thing)) { SchemaError = SchemaErrors.TypeMismatch; return(false); } } } if (value.ContainsKey("maxItems") && array.Count() >= Convert.ToInt64(value["maxItems"])) { return(false); } if (value.ContainsKey("minItems") && array.Count() < Convert.ToInt64(value["minItems"])) { return(false); } if (value.ContainsKey("uniqueItems") && Convert.ToBoolean(value["uniqueItems"])) { var distinct = array.Distinct(); if (array.Count() != distinct.Count()) { return(false); } } } if (type == "string") { if (value.ContainsKey("pattern") && !Regex.IsMatch(item as string, value["pattern"] as string, RegexOptions.IgnoreCase)) { SchemaError = SchemaErrors.PatternMismatch; return(false); } if (value.ContainsKey("maxLength") && (item as string).Length > Convert.ToInt64(value["maxLength"])) { return(false); } if (value.ContainsKey("minLength") && (item as string).Length < Convert.ToInt64(value["minLength"])) { return(false); } } if (type == "integer" || type == "number") { var lastError = SchemaError; SchemaError = SchemaErrors.NumberMismatch; var val = Convert.ToDouble(item); if (value.ContainsKey("multipleOf") && val % Convert.ToDouble(value["multipleOf"]) != 0) { return(false); } if (value.ContainsKey("maximum")) { var max = Convert.ToDouble(value["maximum"]); if (value.ContainsKey("exclusiveMaximum") && Convert.ToBoolean(value["exclusiveMaximum"]) && val >= max) { return(false); } else if (val > max) { return(false); } } if (value.ContainsKey("minimum")) { var min = Convert.ToDouble(value["minimum"]); if (value.ContainsKey("exclusiveMinimum") && Convert.ToBoolean(value["exclusiveMinimum"]) && val <= min) { return(false); } else if (val < min) { return(false); } } SchemaError = lastError; } } } return(true); }
private void ValidationCallback(object sender, ValidationEventArgs args) { SchemaErrors.Add(new ErrorInfo(sender, args)); }