Ejemplo n.º 1
0
 private static void AddSyntaxErrors(JSONDocument document, JsonErrorLocation location, List <JsonError> errors)
 {
     foreach (Tuple <JSONParseItem, JSONParseError> parserError in document.GetContainedParseErrors())
     {
         errors.Add(new JsonError
         {
             Kind     = JsonErrorKind.Syntax,
             Start    = parserError.Item1.Start,
             Length   = parserError.Item1.Length,
             Location = location,
             Message  = parserError.Item2.Text ?? parserError.Item2.ErrorType.ToString()
         });
     }
 }
Ejemplo n.º 2
0
        public static async Task <ValidationResult> ValidateAsync(StringWithFileNameTextProvider instanceTextProvider, StringWithFileNameTextProvider schemaTextProvider, IEnumerable <IJSONSchemaFormatHandler> formatHandlers)
        {
            JSONDocumentLoader loader      = new JSONDocumentLoader();
            JSONParser         parser      = new JSONParser();
            JSONDocument       schemaDoc   = parser.Parse(schemaTextProvider);
            JSONDocument       instanceDoc = parser.Parse(instanceTextProvider);
            List <JSONError>   allErrors   = new List <JSONError>();

            foreach (Tuple <JSONParseItem, JSONParseError> error in instanceDoc.GetContainedParseErrors())
            {
                allErrors.Add(new JSONError
                {
                    Kind     = JSONErrorKind.Syntax,
                    Length   = error.Item1.Length,
                    Start    = error.Item1.Start,
                    Location = JSONErrorLocation.InstanceDocument,
                    Message  = error.Item2.Text ?? error.Item2.ErrorType.ToString()
                });
            }

            foreach (Tuple <JSONParseItem, JSONParseError> error in schemaDoc.GetContainedParseErrors())
            {
                allErrors.Add(new JSONError
                {
                    Kind     = JSONErrorKind.Syntax,
                    Length   = error.Item1.Length,
                    Start    = error.Item1.Start,
                    Location = JSONErrorLocation.Schema,
                    Message  = error.Item2.Text ?? error.Item2.ErrorType.ToString()
                });
            }

            loader.SetCacheItem(new JSONDocumentLoadResult(schemaDoc));
            loader.SetCacheItem(new JSONDocumentLoadResult(instanceDoc));

            JSONSchemaDraft4EvaluationTreeNode tree = await JSONSchemaDraft4EvaluationTreeProducer.CreateEvaluationTreeAsync(instanceDoc, (JSONObject)schemaDoc.TopLevelValue, loader, formatHandlers).ConfigureAwait(false);

            foreach (JSONSchemaValidationIssue issue in tree.ValidationIssues)
            {
                allErrors.Add(new JSONError
                {
                    Kind     = JSONErrorKind.Validation,
                    Start    = issue.TargetItem.Start,
                    Length   = issue.TargetItem.Length,
                    Location = JSONErrorLocation.InstanceDocument,
                    Message  = issue.Message
                });
            }

            foreach (JSONSchemaSanityIssue issue in tree.SanityIssues)
            {
                allErrors.Add(new JSONError
                {
                    Kind     = JSONErrorKind.Validation,
                    Start    = issue.ParseItem.Start,
                    Length   = issue.ParseItem.Length,
                    Location = JSONErrorLocation.Schema,
                    Message  = issue.Message
                });
            }

            return(new ValidationResult
            {
                InstanceDocumentText = instanceTextProvider.Text,
                SchemaText = schemaTextProvider.Text,
                Errors = allErrors
            });
        }