private JsonValue _Resolve(JsonValue root)
        {
            var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None);
            var address        = string.IsNullOrWhiteSpace(referenceParts[0]) ? DocumentPath?.OriginalString : referenceParts[0];
            var fragment       = referenceParts.Length > 1 ? referenceParts[1] : string.Empty;
            var jValue         = root;

            if (!string.IsNullOrWhiteSpace(address))
            {
                if (!Uri.TryCreate(address, UriKind.Absolute, out Uri absolute))
                {
                    address = Id + address;
                }
                if (DocumentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute))
                {
                    var uriFolder = DocumentPath.OriginalString.EndsWith("/") ? DocumentPath : DocumentPath.GetParentUri();
                    absolute = new Uri(uriFolder, address);
                    address  = absolute.OriginalString;
                }
                jValue = JsonSchemaRegistry.Get(address).ToJson(null);
            }
            if (jValue == null)
            {
                return(root);
            }
            if (jValue == "#")
            {
                throw new ArgumentException("Cannot use a root reference as the base schema.");
            }

            Resolved = _ResolveLocalReference(jValue, fragment, string.IsNullOrWhiteSpace(address) ? null : new Uri(address));
            return(jValue);
        }
Exemple #2
0
        private void _ResolveReference(SchemaValidationContext context)
        {
            var documentPath   = context.BaseUri;
            var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None);
            var address        = string.IsNullOrWhiteSpace(referenceParts[0]) ? documentPath?.OriginalString : referenceParts[0];

            _resolvedFragment = referenceParts.Length > 1 ? JsonPointer.Parse(referenceParts[1]) : new JsonPointer();
            if (!string.IsNullOrWhiteSpace(address))
            {
                if (!Uri.TryCreate(address, UriKind.Absolute, out var absolute))
                {
                    address = context.Local.Id + address;
                }

                if (documentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute))
                {
                    var uriFolder = documentPath.OriginalString.EndsWith("/") ? documentPath : documentPath.GetParentUri();
                    absolute = new Uri(uriFolder, address);
                    address  = absolute.OriginalString;
                }

                _resolvedRoot = JsonSchemaRegistry.Get(address);
            }
            else
            {
                _resolvedRoot = context.Root;
            }

            _ResolveLocalReference(_resolvedRoot?.DocumentPath ?? context.BaseUri);
        }
Exemple #3
0
        private void _ResolveReference(SchemaValidationContext context)
        {
            if (context.RecursiveAnchor != null)
            {
                var baseDocument = JsonSchemaRegistry.Get(context.BaseUri.OriginalString);
                if (baseDocument?.Get <RecursiveAnchorKeyword>() != null)
                {
                    _resolvedRoot = context.RecursiveAnchor;
                }
            }

            if (Reference.IsLocalSchemaId())
            {
                Resolved = context.LocalRegistry.GetLocal(Reference);
                if (Resolved != null)
                {
                    return;
                }
            }

            var documentPath   = _resolvedRoot?.DocumentPath ?? context.BaseUri;
            var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None);
            var address        = string.IsNullOrWhiteSpace(referenceParts[0]) ? documentPath?.OriginalString : referenceParts[0];

            _resolvedFragment = referenceParts.Length > 1 ? JsonPointer.Parse(referenceParts[1]) : new JsonPointer();
            if (_resolvedRoot == null)
            {
                if (!string.IsNullOrWhiteSpace(address))
                {
                    if (!Uri.TryCreate(address, UriKind.Absolute, out var absolute))
                    {
                        address = context.Local.Id + address;
                    }

                    if (documentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute))
                    {
                        var uriFolder = documentPath.OriginalString.EndsWith("/") ? documentPath : documentPath.GetParentUri();
                        absolute = new Uri(uriFolder, address);
                        address  = absolute.OriginalString;
                    }

                    _resolvedRoot = JsonSchemaRegistry.Get(address);
                }
                else
                {
                    _resolvedRoot = context.Root;
                }
            }

            var wellKnown = JsonSchemaRegistry.GetWellKnown(Reference);

            if (wellKnown != null)
            {
                Resolved = wellKnown;
                return;
            }

            _ResolveLocalReference(_resolvedRoot?.DocumentPath ?? context.BaseUri);
        }
Exemple #4
0
        /// <summary>
        /// Provides the validation logic for this keyword.
        /// </summary>
        /// <param name="context">The context object.</param>
        /// <returns>Results object containing a final result and any errors that may have been found.</returns>
        public SchemaValidationResults Validate(SchemaValidationContext context)
        {
            if (!context.IsMetaSchemaValidation)
            {
                return(SchemaValidationResults.Null);
            }

            var nestedResults = new List <SchemaValidationResults>();

            var allVocabularies = this.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            allVocabularies[SchemaVocabularies.Core] = true;

            foreach (var kvp in allVocabularies)
            {
                var vocabulary = kvp.Key;
                if (vocabulary.MetaSchemaId == context.Local.Id)
                {
                    continue;
                }

                var required = kvp.Value;
                if (vocabulary.MetaSchemaId != null)
                {
                    var newContext = new SchemaValidationContext(context)
                    {
                        BaseRelativeLocation = context.BaseRelativeLocation?.CloneAndAppend(Name, vocabulary.Id),
                        RelativeLocation     = context.RelativeLocation.CloneAndAppend(Name, vocabulary.Id),
                    };
                    var metaSchema = JsonSchemaRegistry.Get(vocabulary.MetaSchemaId);
                    if (metaSchema != null)
                    {
                        metaSchema.Validate(newContext);
                    }
                    else if (required)
                    {
                        nestedResults.Add(new SchemaValidationResults(Name, newContext));
                    }
                }
            }

            var results = new SchemaValidationResults(Name, context)
            {
                NestedResults = nestedResults,
                IsValid       = nestedResults.All(r => r.IsValid)
            };

            return(results);
        }
Exemple #5
0
        private void _ResolveReference(SchemaValidationContext context)
        {
            Log.Schema($"Resolving `{Reference}`");

            if (context.RecursiveAnchor != null)
            {
                Log.Schema("Finding anchor of root schema");
                if (context.BaseUri == null)
                {
                    throw new InvalidOperationException("BaseUri not set");
                }
                var baseDocument = JsonSchemaRegistry.Get(context.BaseUri.OriginalString);
                if (baseDocument?.Get <RecursiveAnchorKeyword>() != null)
                {
                    _resolvedRoot = context.RecursiveAnchor;
                }
            }

            if (Reference.IsLocalSchemaId())
            {
                Log.Schema("Reference recognized as anchor or local ID");
                Resolved = context.LocalRegistry.GetLocal(Reference);
                if (Resolved != null)
                {
                    return;
                }

                Log.Schema($"`{Reference}` is an unknown anchor");
            }

            var documentPath   = _resolvedRoot?.DocumentPath ?? context.BaseUri;
            var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None);
            var address        = string.IsNullOrWhiteSpace(referenceParts[0]) ? documentPath?.OriginalString : referenceParts[0];

            _resolvedFragment = referenceParts.Length > 1 ? JsonPointer.Parse(referenceParts[1]) : new JsonPointer();
            if (_resolvedRoot == null)
            {
                if (!string.IsNullOrWhiteSpace(address))
                {
                    if (!Uri.TryCreate(address, UriKind.Absolute, out _))
                    {
                        address = context.Local.Id + address;
                    }

                    if (documentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out _))
                    {
                        var uriFolder = documentPath.OriginalString.EndsWith("/") ? documentPath : documentPath.GetParentUri();
                        var absolute  = new Uri(uriFolder, address);
                        address = absolute.OriginalString;
                    }

                    _resolvedRoot = JsonSchemaRegistry.Get(address);
                }
                else
                {
                    _resolvedRoot = context.Root;
                }
            }

            if (_resolvedRoot == null)
            {
                Log.Schema("Could not resolve root of reference");
                return;
            }

            var wellKnown = JsonSchemaRegistry.GetWellKnown(Reference);

            if (wellKnown != null)
            {
                Log.Schema("Well known reference found");
                Resolved = wellKnown;
                return;
            }

            _ResolveLocalReference(_resolvedRoot?.DocumentPath ?? context.BaseUri !);
        }
Exemple #6
0
        private void _ResolveReference(SchemaValidationContext context)
        {
            Log.Schema($"Resolving `{Reference}`");

            if (Reference.IsLocalSchemaId())
            {
                Log.Schema("Reference recognized as anchor or local ID");
                Resolved = context.LocalRegistry.GetLocal(Reference);
                if (Resolved != null)
                {
                    return;
                }

                Log.Schema($"`{Reference}` is an unknown anchor");
            }

            var documentPath   = context.BaseUri;
            var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None);
            var address        = string.IsNullOrWhiteSpace(referenceParts[0]) ? documentPath?.OriginalString.Split('#')[0] : referenceParts[0];

            _resolvedFragment = referenceParts.Length > 1 ? JsonPointer.Parse(referenceParts[1]) : new JsonPointer();
            if (!string.IsNullOrWhiteSpace(address))
            {
                if (!Uri.TryCreate(address, UriKind.Absolute, out var absolute) &&
                    (JsonSchemaOptions.RefResolution == RefResolutionStrategy.ProcessSiblingId ||
                     context.Root.SupportedVersions == JsonSchemaVersion.Draft2019_09))
                {
                    address = context.Local.Id + address;
                }

                if (documentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute))
                {
                    var uriFolder = documentPath.OriginalString.EndsWith("/") ? documentPath : documentPath.GetParentUri();
                    absolute = new Uri(uriFolder, address);
                    address  = absolute.OriginalString;
                }

                _resolvedRoot = JsonSchemaRegistry.Get(address);
            }
            else
            {
                _resolvedRoot = context.Root;
            }

            if (_resolvedRoot == null)
            {
                Log.Schema("Could not resolve root of reference");
                return;
            }

            var wellKnown = JsonSchemaRegistry.GetWellKnown(Reference);

            if (wellKnown != null)
            {
                Log.Schema("Well known reference found");
                Resolved = wellKnown;
                return;
            }

            _ResolveLocalReference(_resolvedRoot?.DocumentPath ?? context.BaseUri !);
        }