コード例 #1
0
        protected bool TestType <T>(JSchema currentSchema, JSchemaType currentType, T value)
        {
            if (!JSchemaTypeHelpers.HasFlag(currentSchema.Type, currentType))
            {
                RaiseError($"Invalid type. Expected {currentSchema.Type.Value.GetDisplayText()} but got {currentType.GetDisplayText()}.", ErrorType.Type, currentSchema, value, null);
                return(false);
            }

            return(true);
        }
コード例 #2
0
        internal static bool TestType <T>(SchemaScope scope, JSchema currentSchema, JSchemaType currentType, T?value)
        {
            if (!JSchemaTypeHelpers.HasFlag(currentSchema.Type, currentType))
            {
                scope.RaiseError($"Invalid type. Expected {currentSchema.Type.GetValueOrDefault().GetDisplayText()} but got {currentType.GetDisplayText()}.", ErrorType.Type, currentSchema, value, null);
                return(false);
            }

            return(true);
        }
コード例 #3
0
        protected bool TestType(JSchema currentSchema, JSchemaType currentType, object value)
        {
            if (!JSchemaTypeHelpers.HasFlag(currentSchema.Type, currentType))
            {
                RaiseError("Invalid type. Expected {0} but got {1}.".FormatWith(CultureInfo.InvariantCulture, currentSchema.Type, currentType), ErrorType.Type, currentSchema, value, null);
                return(false);
            }

            return(true);
        }
コード例 #4
0
ファイル: SchemaScope.cs プロジェクト: ljcom/bot
        protected bool TestType(JSchema currentSchema, JSchemaType currentType, object value)
        {
            if (!JSchemaTypeHelpers.HasFlag(currentSchema.Type, currentType))
            {
                RaiseError($"Invalid type. Expected {currentSchema.Type} but got {currentType}.", ErrorType.Type, currentSchema, value, null);
                return(false);
            }

            return(true);
        }
コード例 #5
0
        private bool ValidateNumber(JSchema schema, object value)
        {
            if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer) &&
                !JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Number) &&
                MathHelpers.IsDoubleMultiple(value, 1) &&
                SchemaVersionHelpers.EnsureVersion(Context.Validator.SchemaVersion, SchemaVersion.Draft6))
            {
                // accept values like 1.0 in draft6+
            }
            else if (!TestType(schema, JSchemaType.Number, value))
            {
                return(false);
            }

            if (schema.Maximum != null)
            {
                double d = Convert.ToDouble(value, CultureInfo.InvariantCulture);

                if (d > schema.Maximum)
                {
                    RaiseError($"Float {JsonConvert.ToString(value)} exceeds maximum value of {schema.Maximum}.", ErrorType.Maximum, schema, value, null);
                }
                if (schema.ExclusiveMaximum && d == schema.Maximum)
                {
                    RaiseError($"Float {JsonConvert.ToString(value)} equals maximum value of {schema.Maximum} and exclusive maximum is true.", ErrorType.Maximum, schema, value, null);
                }
            }

            if (schema.Minimum != null)
            {
                double d = Convert.ToDouble(value, CultureInfo.InvariantCulture);

                if (d < schema.Minimum)
                {
                    RaiseError($"Float {JsonConvert.ToString(value)} is less than minimum value of {schema.Minimum}.", ErrorType.Minimum, schema, value, null);
                }
                if (schema.ExclusiveMinimum && d == schema.Minimum)
                {
                    RaiseError($"Float {JsonConvert.ToString(value)} equals minimum value of {schema.Minimum} and exclusive minimum is true.", ErrorType.Minimum, schema, value, null);
                }
            }

            if (schema.MultipleOf != null)
            {
                bool isMultiple = MathHelpers.IsDoubleMultiple(value, schema.MultipleOf.Value);

                if (!isMultiple)
                {
                    RaiseError($"Float {JsonConvert.ToString(value)} is not a multiple of {schema.MultipleOf}.", ErrorType.MultipleOf, schema, value, null);
                }
            }

            return(true);
        }
コード例 #6
0
        private void PopulatePrimativeSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired)
        {
            Type        nonNullableUnderlyingType = GetNonNullableUnderlyingType(contract);
            JSchemaType type = GetJSchemaType(contract.UnderlyingType, valueRequired);

            if (type != Constants.AnyType)
            {
                schema.Type = GetJSchemaType(contract.UnderlyingType, valueRequired);
            }

            if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
            {
                if (AttributeHelpers.GetStringLength(memberProperty, out int minimumLength, out int maximumLength))
                {
                    schema.MinimumLength = minimumLength;
                    schema.MaximumLength = maximumLength;
                }
                else
                {
                    schema.MinimumLength = AttributeHelpers.GetMinLength(memberProperty);
                    schema.MaximumLength = AttributeHelpers.GetMaxLength(memberProperty);
                }

                schema.Pattern = AttributeHelpers.GetPattern(memberProperty);
                schema.Format  = AttributeHelpers.GetFormat(memberProperty);

                // no format specified, derive from type
                if (schema.Format == null)
                {
                    if (nonNullableUnderlyingType == typeof(DateTime) ||
                        nonNullableUnderlyingType == typeof(DateTimeOffset))
                    {
                        schema.Format = Constants.Formats.DateTime;
                    }
                    else if (nonNullableUnderlyingType == typeof(Uri))
                    {
                        schema.Format = Constants.Formats.Uri;
                    }
                }
            }
コード例 #7
0
        private void PopulatePrimativeSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired)
        {
            JSchemaType type = GetJSchemaType(contract.UnderlyingType, valueRequired);

            if (type != Constants.AnyType)
            {
                schema.Type = GetJSchemaType(contract.UnderlyingType, valueRequired);
            }

            if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
            {
                int minimumLength;
                int maximumLength;
                if (DataAnnotationHelpers.GetStringLength(memberProperty, out minimumLength, out maximumLength))
                {
                    schema.MinimumLength = minimumLength;
                    schema.MaximumLength = maximumLength;
                }
                else
                {
                    schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty);
                }

                schema.Pattern = DataAnnotationHelpers.GetPattern(memberProperty);
                schema.Format  = DataAnnotationHelpers.GetFormat(memberProperty);
            }
            if (JSchemaTypeHelpers.HasFlag(type, JSchemaType.Number) || JSchemaTypeHelpers.HasFlag(type, JSchemaType.Integer))
            {
                double minimum;
                double maximum;
                if (DataAnnotationHelpers.GetRange(memberProperty, out minimum, out maximum))
                {
                    schema.Minimum = minimum;
                    schema.Maximum = maximum;
                }
            }

            if (JSchemaTypeHelpers.HasFlag(type, JSchemaType.Integer) &&
                contract.NonNullableUnderlyingType.IsEnum() &&
                ReflectionUtils.GetAttribute <FlagsAttribute>(contract.NonNullableUnderlyingType) == null)
            {
                if ((type & JSchemaType.Null) == JSchemaType.Null)
                {
                    schema.Enum.Add(JValue.CreateNull());
                }

                IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(contract.NonNullableUnderlyingType);
                foreach (EnumValue <long> enumValue in enumValues)
                {
                    JToken value = JToken.FromObject(enumValue.Value);

                    schema.Enum.Add(value);
                }
            }

            Type enumDataType = DataAnnotationHelpers.GetEnumDataType(memberProperty);

            if (enumDataType != null && CollectionUtils.IsNullOrEmpty(schema._enum))
            {
                IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(enumDataType);
                foreach (EnumValue <long> enumValue in enumValues)
                {
                    JToken value = (JSchemaTypeHelpers.HasFlag(type, JSchemaType.String))
                        ? enumValue.Name
                        : JToken.FromObject(enumValue.Value);

                    schema.Enum.Add(value);
                }
            }
        }
コード例 #8
0
        private JSchema PopulateSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired)
        {
            schema.Title       = GetTitle(contract.NonNullableUnderlyingType);
            schema.Description = GetDescription(contract.NonNullableUnderlyingType);

            JsonConverter converter = contract.Converter ?? contract.InternalConverter;

            if (converter != null)
            {
                // todo: Add GetSchema to JsonConverter and use here?
                schema.Type = JSchemaType.Any;
            }
            else
            {
                switch (contract.ContractType)
                {
                case JsonContractType.Object:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    GenerateObjectSchema(schema, contract.NonNullableUnderlyingType, (JsonObjectContract)contract);
                    break;

                case JsonContractType.Array:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type         = AddNullType(JSchemaType.Array, valueRequired);
                    schema.MinimumItems = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumItems = DataAnnotationHelpers.GetMaxLength(memberProperty);

                    JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(contract.NonNullableUnderlyingType);
                    bool allowNullItem = (arrayAttribute == null || arrayAttribute.AllowNullItems);

                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(contract.NonNullableUnderlyingType);
                    if (collectionItemType != null)
                    {
                        schema.Items.Add(GenerateInternal(collectionItemType, (!allowNullItem) ? Required.Always : Required.Default, null, (JsonArrayContract)contract));
                    }
                    break;

                case JsonContractType.Primitive:
                    schema.Type = GetJSchemaType(contract.UnderlyingType, valueRequired);

                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
                    {
                        int minimumLength;
                        int maximumLength;
                        if (DataAnnotationHelpers.GetStringLength(memberProperty, out minimumLength, out maximumLength))
                        {
                            schema.MinimumLength = minimumLength;
                            schema.MaximumLength = maximumLength;
                        }
                        else
                        {
                            schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty);
                            schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty);
                        }

                        schema.Pattern = DataAnnotationHelpers.GetPattern(memberProperty);
                        schema.Format  = DataAnnotationHelpers.GetFormat(memberProperty);
                    }
                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Float) || JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer))
                    {
                        double minimum;
                        double maximum;
                        if (DataAnnotationHelpers.GetRange(memberProperty, out minimum, out maximum))
                        {
                            schema.Minimum = minimum;
                            schema.Maximum = maximum;
                        }
                    }

                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer) &&
                        contract.NonNullableUnderlyingType.IsEnum() &&
                        ReflectionUtils.GetAttribute <FlagsAttribute>(contract.NonNullableUnderlyingType) == null)
                    {
                        IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(contract.NonNullableUnderlyingType);
                        foreach (EnumValue <long> enumValue in enumValues)
                        {
                            JToken value = JToken.FromObject(enumValue.Value);

                            schema.Enum.Add(value);
                        }
                    }

                    Type enumDataType = DataAnnotationHelpers.GetEnumDataType(memberProperty);
                    if (enumDataType != null && CollectionUtils.IsNullOrEmpty(schema._enum))
                    {
                        IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(enumDataType);
                        foreach (EnumValue <long> enumValue in enumValues)
                        {
                            JToken value = (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
                                    ? enumValue.Name
                                    : JToken.FromObject(enumValue.Value);

                            schema.Enum.Add(value);
                        }
                    }
                    break;

                case JsonContractType.String:
                    JSchemaType schemaType = (!ReflectionUtils.IsNullable(contract.UnderlyingType))
                            ? JSchemaType.String
                            : AddNullType(JSchemaType.String, valueRequired);

                    schema.Type          = schemaType;
                    schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty);
                    break;

                case JsonContractType.Dictionary:
                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    schema.MinimumProperties = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumProperties = DataAnnotationHelpers.GetMaxLength(memberProperty);

                    Type keyType;
                    Type valueType;
                    ReflectionUtils.GetDictionaryKeyValueTypes(contract.NonNullableUnderlyingType, out keyType, out valueType);

                    if (keyType != null)
                    {
                        JsonContract keyContract = ContractResolver.ResolveContract(keyType);

                        // can be converted to a string
                        if (keyContract.ContractType == JsonContractType.Primitive)
                        {
                            schema.AdditionalProperties = GenerateInternal(valueType, Required.Default, null, (JsonDictionaryContract)contract);
                        }
                    }
                    break;

                case JsonContractType.Serializable:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    schema.AllowAdditionalProperties = true;
                    break;

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                    schema.Type = JSchemaType.Any;
                    break;

                default:
                    throw new JsonException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, contract));
                }
            }

            return(schema);
        }