internal static bool HasAttribute <T>(this JsonProperty jsonProperty) where T : Attribute { var memberInfo = jsonProperty.MemberInfo(); return(memberInfo != null && memberInfo.GetCustomAttribute <T>() != null); }
internal static Schema AssignValidationProperties(this Schema schema, JsonProperty jsonProperty) { var propInfo = jsonProperty.MemberInfo(); if (propInfo == null) { return(schema); } foreach (var attribute in propInfo.GetCustomAttributes(false)) { if (attribute is DefaultValueAttribute defaultValue) { schema.Default = defaultValue.Value; } if (attribute is RegularExpressionAttribute regex) { schema.Pattern = regex.Pattern; } if (attribute is RangeAttribute range) { if (Int32.TryParse(range.Maximum.ToString(), out int maximum)) { schema.Maximum = maximum; } if (Int32.TryParse(range.Minimum.ToString(), out int minimum)) { schema.Minimum = minimum; } } if (attribute is MinLengthAttribute minLength) { schema.MinLength = minLength.Length; } if (attribute is MaxLengthAttribute maxLength) { schema.MaxLength = maxLength.Length; } if (attribute is StringLengthAttribute stringLength) { schema.MinLength = stringLength.MinimumLength; schema.MaxLength = stringLength.MaximumLength; } if (attribute is DataTypeAttribute dataTypeAttribute && schema.Type == "string") { if (DataTypeFormatMap.TryGetValue(dataTypeAttribute.DataType, out string format)) { schema.Format = format; } } } if (!jsonProperty.Writable) { schema.ReadOnly = true; } return(schema); }
internal static Schema AssignValidationProperties(this Schema schema, JsonProperty jsonProperty) { var propInfo = jsonProperty.MemberInfo(); if (propInfo == null) { return(schema); } foreach (var attribute in propInfo.GetCustomAttributes(false)) { var defaultValue = attribute as DefaultValueAttribute; if (defaultValue != null) { schema.Default = defaultValue.Value; } var regex = attribute as RegularExpressionAttribute; if (regex != null) { schema.Pattern = regex.Pattern; } var range = attribute as RangeAttribute; if (range != null) { int maximum; if (Int32.TryParse(range.Maximum.ToString(), out maximum)) { schema.Maximum = maximum; } int minimum; if (Int32.TryParse(range.Minimum.ToString(), out minimum)) { schema.Minimum = minimum; } } var minLength = attribute as MinLengthAttribute; if (minLength != null) { schema.MinLength = minLength.Length; } var maxLength = attribute as MaxLengthAttribute; if (maxLength != null) { schema.MaxLength = maxLength.Length; } var stringLength = attribute as StringLengthAttribute; if (stringLength != null) { schema.MinLength = stringLength.MinimumLength; schema.MaxLength = stringLength.MaximumLength; } } if (!jsonProperty.Writable) { schema.ReadOnly = true; } return(schema); }