Esempio n. 1
0
        private static IEnumerable <JsonError> Validate(string instanceText, string schemaText)
        {
            List <JsonError> errors = new List <JsonError>();

            JSONDocument instanceDocument = JSONParser.Parse(instanceText);

            AddSyntaxErrors(instanceDocument, JsonErrorLocation.InstanceDocument, errors);

            JSONDocument schemaDocument = JSONParser.Parse(schemaText);

            AddSyntaxErrors(schemaDocument, JsonErrorLocation.Schema, errors);

            var loader = new JSONDocumentLoader();

            loader.SetCacheItem(new JSONDocumentLoadResult(instanceDocument));
            loader.SetCacheItem(new JSONDocumentLoadResult(schemaDocument));

            JSONSchemaDraft4EvaluationTreeNode tree =
                JSONSchemaDraft4EvaluationTreeProducer.CreateEvaluationTreeAsync(
                    instanceDocument,
                    (JSONObject)schemaDocument.TopLevelValue,
                    loader,
                    Enumerable.Empty <IJSONSchemaFormatHandler>()).Result;

            AddValidationErrors(tree.ValidationIssues, errors);

            return(errors);
        }
Esempio n. 2
0
        public static JSONCompletionOptionResult GetCompletionOptions(StringWithFileNameTextProvider instanceTextProvider, StringWithFileNameTextProvider schemaTextProvider, IEnumerable <IJSONSchemaFormatHandler> formatHandlers, int cursorPosition)
        {
            JSONDocumentLoader loader      = new JSONDocumentLoader();
            JSONParser         parser      = new JSONParser();
            JSONDocument       schemaDoc   = parser.Parse(schemaTextProvider);
            JSONDocument       instanceDoc = parser.Parse(instanceTextProvider);
            List <JSONOption>  allOptions  = new List <JSONOption>();

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

            JSONSchemaDraft4EvaluationTreeNode tree = JSONSchemaDraft4EvaluationTreeProducer.CreateEvaluationTreeAsync(instanceDoc, (JSONObject)schemaDoc.TopLevelValue, loader, formatHandlers).Result;
            var parseItem = instanceDoc.ItemBeforePosition(cursorPosition);

            var convertToReportMethod = typeof(JSONSchemaDraft4Evaluator).GetMethod("ConvertToReport", BindingFlags.Static | BindingFlags.NonPublic);
            var report = (JSONSchemaDraft4EvaluationReport)convertToReportMethod.Invoke(null, new object[] { DateTime.Now, tree });

            if (parseItem?.PreviousSibling is JSONMember && parseItem.PreviousSibling.IsUnclosed)
            {
                parseItem = parseItem.PreviousSibling;
            }

            var originalItem = parseItem;

            while (parseItem is JSONTokenItem)
            {
                parseItem = parseItem.Parent;
            }

            JSONMember member = parseItem as JSONMember;

            if (member != null)
            {
                //Don't glue completion to the previous member's value if calling up completion after a comma
                if ((member.Comma == null || cursorPosition < member.Comma.AfterEnd) && member.Name != originalItem)
                {
                    var options = report.GetValueOptionsAsync(member, cursorPosition).Result;

                    foreach (var option in options)
                    {
                        allOptions.Add(new JSONOption
                        {
                            InsertionText = option.InsertionText,
                            DisplayText   = option.DisplayText,
                            Type          = option.Format
                        });
                    }

                    return(new JSONCompletionOptionResult
                    {
                        ReplaceStart = member.Value?.Start ?? member.Colon.AfterEnd,
                        ReplaceLength = member.Value?.Length ?? 0,
                        Options = allOptions
                    });
                }

                parseItem = parseItem.Parent;
            }

            JSONArrayElement arrayElement = parseItem as JSONArrayElement;

            if (arrayElement != null)
            {
                JSONArray arr   = (JSONArray)arrayElement.Parent;
                int       index = arr.Elements.IndexOf(arrayElement);

                var options = report.GetValueOptionsAsync(arr, index, cursorPosition).Result;

                foreach (var option in options)
                {
                    allOptions.Add(new JSONOption
                    {
                        InsertionText = option.InsertionText,
                        DisplayText   = option.DisplayText,
                        Type          = option.TypeDescription
                    });
                }

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = cursorPosition,
                    ReplaceLength = 0,
                    Options = allOptions
                });
            }

            JSONArray array = parseItem as JSONArray;

            if (array != null)
            {
                int index;
                for (index = 0; index < array.Elements.Count && cursorPosition > array.Elements[index].AfterEnd; ++index)
                {
                }

                var options = report.GetValueOptionsAsync(array, index, cursorPosition).Result;

                foreach (var option in options)
                {
                    allOptions.Add(new JSONOption
                    {
                        InsertionText = option.InsertionText,
                        DisplayText   = option.DisplayText,
                        Type          = option.TypeDescription
                    });
                }

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = cursorPosition,
                    ReplaceLength = 0,
                    Options = allOptions
                });
            }

            JSONObject obj = parseItem as JSONObject;

            if (obj != null)
            {
                var options = report.GetPropertyInfosAsync(obj).Result;

                foreach (var option in options)
                {
                    JSONMember existingMember = obj.Members.FirstOrDefault(x => x.CanonicalizedNameText == option.DisplayText);
                    if (existingMember == null || (member != null && existingMember == member && cursorPosition < existingMember.AfterEnd))
                    {
                        allOptions.Add(new JSONOption
                        {
                            InsertionText = option.InsertionText,
                            DisplayText   = option.DisplayText,
                            Type          = option.TypeDescription
                        });
                    }
                }

                int replaceStart  = member?.AfterEnd >= cursorPosition ? member.Name.Start : cursorPosition;
                int replaceLength = member?.AfterEnd >= cursorPosition ? member.Name.Length : 0;

                return(new JSONCompletionOptionResult
                {
                    ReplaceStart = replaceStart,
                    ReplaceLength = replaceLength,
                    Options = allOptions
                });
            }

            return(new JSONCompletionOptionResult
            {
                Options = new List <JSONOption>()
            });
        }
Esempio n. 3
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
            });
        }