예제 #1
0
        private bool IsPropertyDefinied(JSchema schema, string propertyName)
        {
            if (schema._properties != null && schema._properties.ContainsKey(propertyName))
            {
                return(true);
            }

            if (schema._patternProperties != null)
            {
                foreach (PatternSchema patternSchema in schema.GetPatternSchemas())
                {
                    Regex  regex;
                    string errorMessage;
                    if (patternSchema.TryGetPatternRegex(out regex, out errorMessage))
                    {
                        if (regex.IsMatch(_currentPropertyName))
                        {
                            if (Regex.IsMatch(propertyName, patternSchema.Pattern))
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }
예제 #2
0
        private bool IsPropertyDefinied(JSchema schema, string propertyName)
        {
            if (schema._properties != null && schema._properties.ContainsKey(propertyName))
            {
                return(true);
            }

            if (schema._patternProperties != null)
            {
                foreach (PatternSchema patternSchema in schema.GetPatternSchemas())
                {
                    if (patternSchema.TryGetPatternRegex(
#if !(NET35 || NET40)
                            Context.Validator.RegexMatchTimeout,
#endif
                            out Regex regex,
                            out string _))
                    {
                        if (RegexHelpers.IsMatch(regex, patternSchema.Pattern, propertyName))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        private bool IsPropertyDefinied(JSchema schema, string propertyName)
        {
            if (schema._properties != null && schema._properties.ContainsKey(propertyName))
            {
                return true;
            }

            if (schema._patternProperties != null)
            {
                foreach (PatternSchema patternSchema in schema.GetPatternSchemas())
                {
                    Regex regex;
                    string errorMessage;
                    if (patternSchema.TryGetPatternRegex(out regex, out errorMessage))
                    {
                        if (regex.IsMatch(_currentPropertyName))
                        {
                            if (Regex.IsMatch(propertyName, patternSchema.Pattern))
                            {
                                return true;
                            }
                        }
                    }
                }
            }

            return false;
        }
예제 #4
0
        private void ProcessSchemaName(ref JsonReader reader, bool isRoot, JSchema schema, string name)
        {
            switch (name)
            {
            case Constants.PropertyNames.Id:
                schema.Id = ReadUri(reader, name);
                break;

            case Constants.PropertyNames.Ref:
                schema.Reference = ReadUri(reader, name);
                break;

            case Constants.PropertyNames.Properties:
                schema._properties = new JSchemaDictionary(schema);
                ReadProperties(reader, schema._properties);

                // add schemas with deprecated required flag to new required array
                foreach (KeyValuePair <string, JSchema> schemaProperty in schema._properties)
                {
                    if (schemaProperty.Value.DeprecatedRequired)
                    {
                        if (!schema.Required.Contains(schemaProperty.Key))
                        {
                            schema.Required.Add(schemaProperty.Key);
                        }
                    }
                }
                break;

            case Constants.PropertyNames.Items:
                ReadItems(reader, schema);
                break;

            case Constants.PropertyNames.Type:
            {
                object typeResult = ReadType(reader, schema, name);
                if (typeResult is JSchemaType)
                {
                    schema.Type = (JSchemaType)typeResult;
                }
                else
                {
                    schema._anyOf = (JSchemaCollection)typeResult;
                }
                break;
            }

            case Constants.PropertyNames.AnyOf:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    ReadSchemaArray(reader, schema, name, out schema._anyOf);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.AllOf:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    ReadSchemaArray(reader, schema, name, out schema._allOf);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.OneOf:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    ReadSchemaArray(reader, schema, name, out schema._oneOf);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.Not:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    ReadSchema(reader, name, s => schema.Not = s);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.Title:
                schema.Title = ReadString(reader, name);
                break;

            case Constants.PropertyNames.Description:
                schema.Description = ReadString(reader, name);
                break;

            case Constants.PropertyNames.Format:
                schema.Format = ReadString(reader, name);
                break;

            case Constants.PropertyNames.AdditionalProperties:
                ReadAdditionalProperties(reader, schema);
                break;

            case Constants.PropertyNames.AdditionalItems:
                ReadAdditionalItems(reader, schema);
                break;

            case Constants.PropertyNames.PatternProperties:
                ReadProperties(reader, schema.PatternProperties);

                if (_validationErrors != null)
                {
                    foreach (PatternSchema patternProperty in schema.GetPatternSchemas())
                    {
                        Regex  patternRegex;
                        string errorMessage;
                        if (!patternProperty.TryGetPatternRegex(out patternRegex, out errorMessage))
                        {
                            ValidationError error = ValidationError.CreateValidationError($"Could not parse regex pattern '{patternProperty.Pattern}'. Regex parser error: {errorMessage}", ErrorType.PatternProperties, schema, null, patternProperty.Pattern, null, schema, schema.Path);
                            _validationErrors.Add(error);
                        }
                    }
                }
                break;

            case Constants.PropertyNames.Required:
                ReadRequired(reader, schema);
                break;

            case Constants.PropertyNames.Dependencies:
                ReadDependencies(reader, schema);
                break;

            case Constants.PropertyNames.Minimum:
                schema.Minimum = ReadDouble(reader, name);
                break;

            case Constants.PropertyNames.Maximum:
                schema.Maximum = ReadDouble(reader, name);
                break;

            case Constants.PropertyNames.ExclusiveMinimum:
                schema.ExclusiveMinimum = ReadBoolean(reader, name);
                break;

            case Constants.PropertyNames.ExclusiveMaximum:
                schema.ExclusiveMaximum = ReadBoolean(reader, name);
                break;

            case Constants.PropertyNames.MaximumLength:
                schema.MaximumLength = ReadInteger(reader, name);
                break;

            case Constants.PropertyNames.MinimumLength:
                schema.MinimumLength = ReadInteger(reader, name);
                break;

            case Constants.PropertyNames.MaximumItems:
                schema.MaximumItems = ReadInteger(reader, name);
                break;

            case Constants.PropertyNames.MinimumItems:
                schema.MinimumItems = ReadInteger(reader, name);
                break;

            case Constants.PropertyNames.MaximumProperties:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    schema.MaximumProperties = ReadInteger(reader, name);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.MinimumProperties:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    schema.MinimumProperties = ReadInteger(reader, name);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.DivisibleBy:
                if (EnsureVersion(SchemaVersion.Draft3, SchemaVersion.Draft3))
                {
                    schema.MultipleOf = ReadDouble(reader, name);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.MultipleOf:
                if (EnsureVersion(SchemaVersion.Draft4))
                {
                    schema.MultipleOf = ReadDouble(reader, name);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.Disallow:
            {
                if (EnsureVersion(SchemaVersion.Draft3, SchemaVersion.Draft3))
                {
                    if (schema.Not == null)
                    {
                        schema.Not = new JSchema();
                    }

                    object disallowResult = ReadType(reader, schema, name);
                    if (disallowResult is JSchemaType)
                    {
                        JSchemaType type = schema.Not.Type ?? JSchemaType.None;
                        schema.Not.Type = type | (JSchemaType)disallowResult;
                    }
                    else
                    {
                        schema.Not._anyOf = (JSchemaCollection)disallowResult;
                    }
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;
            }

            case Constants.PropertyNames.Pattern:
                schema.Pattern = ReadString(reader, name);

                if (_validationErrors != null)
                {
                    Regex  patternRegex;
                    string errorMessage;
                    if (!schema.TryGetPatternRegex(out patternRegex, out errorMessage))
                    {
                        ValidationError error = ValidationError.CreateValidationError($"Could not parse regex pattern '{schema.Pattern}'. Regex parser error: {errorMessage}", ErrorType.Pattern, schema, null, schema.Pattern, null, schema, schema.Path);
                        _validationErrors.Add(error);
                    }
                }
                break;

            case Constants.PropertyNames.Enum:
                ReadTokenArray(reader, name, ref schema._enum);
                if (schema._enum.Count == 0)
                {
                    throw JSchemaReaderException.Create(reader, _baseUri, "Enum array must have at least one value.");
                }
                break;

            case Constants.PropertyNames.Extends:
                if (EnsureVersion(SchemaVersion.Draft3, SchemaVersion.Draft3))
                {
                    ReadExtends(reader, schema);
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;

            case Constants.PropertyNames.UniqueItems:
                schema.UniqueItems = ReadBoolean(reader, name);
                break;

            case Constants.PropertyNames.Default:
                EnsureRead(reader, Constants.PropertyNames.Default);
                schema.Default = JToken.ReadFrom(reader);
                break;

            default:
                if (isRoot && name == Constants.PropertyNames.Schema)
                {
                    if (!reader.Read())
                    {
                        throw JSchemaReaderException.Create(reader, _baseUri, "Unexpected end when reading schema.");
                    }

                    if (reader.TokenType != JsonToken.String)
                    {
                        throw JSchemaReaderException.Create(reader, _baseUri, "Unexpected token encountered when reading value for '$schema'. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                    }

                    _schemaVersionUri = new Uri((string)reader.Value, UriKind.RelativeOrAbsolute);

#if !PORTABLE
                    string tempUriText = _schemaVersionUri.OriginalString.ToLower(CultureInfo.InvariantCulture);
#else
                    string tempUriText = _schemaVersionUri.OriginalString.ToLower();
#endif

                    Uri tempUri = new Uri(tempUriText, UriKind.RelativeOrAbsolute);
                    if (tempUri == Constants.SchemaVersions.Draft3)
                    {
                        _schemaVersion = SchemaVersion.Draft3;
                    }
                    else if (tempUri == Constants.SchemaVersions.Draft4)
                    {
                        _schemaVersion = SchemaVersion.Draft4;
                    }

                    if (_validateSchema)
                    {
                        if (_schemaVersion == SchemaVersion.Draft3)
                        {
                            _validatingSchema = SpecSchemaCache.Get("schema-draft-v3.json");
                        }
                        else if (_schemaVersion == SchemaVersion.Draft4)
                        {
                            _validatingSchema = SpecSchemaCache.Get("schema-draft-v4.json");
                        }
                        else
                        {
                            if (!_schemaVersionUri.IsAbsoluteUri)
                            {
                                throw JSchemaReaderException.Create(reader, _baseUri, "Schema version identifier '{0}' is not an absolute URI.".FormatWith(CultureInfo.InvariantCulture, _schemaVersionUri.OriginalString));
                            }

                            _validatingSchema = ResolvedSchema(_schemaVersionUri, _schemaVersionUri);

                            if (_validatingSchema == null)
                            {
                                throw JSchemaReaderException.Create(reader, _baseUri, "Could not resolve schema version identifier '{0}'.".FormatWith(CultureInfo.InvariantCulture, _schemaVersionUri.OriginalString));
                            }
                        }

                        JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
                        validatingReader.Schema = _validatingSchema;
                        // push state that we're already inside an object
                        validatingReader.Validator.ValidateCurrentToken(JsonToken.StartObject, null, 0);
                        validatingReader.ValidationEventHandler += RaiseSchemaValidationError;

                        reader = validatingReader;
                    }
                }
                else
                {
                    ReadExtensionData(reader, schema, name);
                }
                break;
            }
        }