コード例 #1
0
ファイル: SampleData.cs プロジェクト: AndyButland/Totem
            private static string GetExample(CaseInsensitiveDictionary <SchemaObject> schemaDictionary, SchemaObject schemaObject)
            {
                string sampleString;
                string referenceKey = null;
                var    dataType     = schemaObject.GetDataType();

                if (!string.IsNullOrEmpty(schemaObject.Reference))
                {
                    referenceKey = schemaObject.Reference.Replace(@"#/", "");
                }

                // use the Example as the sample data if given
                if (dataType == DataType.Object)
                {
                    sampleString = GenerateSampleString(schemaDictionary, schemaObject.Properties);
                }
                else if (schemaObject.Example != null)
                {
                    if (dataType == DataType.String && schemaObject.GetFormat() == Format.DateTime)
                    {
                        var date = (DateTime)schemaObject.Example;
                        sampleString = $"\"{date:yyyy-MM-ddTHH:mm:ssZ}\"";
                    }
                    else if (dataType == DataType.Array)
                    {
                        var arrayExample = schemaObject.Example.ToString().Replace(Environment.NewLine, "");
                        if (schemaObject.Example.ToString()[0] != '[') // example of a single item instead of a list
                        {
                            arrayExample = $"[{arrayExample}]";
                        }

                        sampleString = arrayExample;
                    }
                    else if (dataType == DataType.String)
                    {
                        sampleString = $"\"{schemaObject.Example}\"";
                    }
                    else
                    {
                        sampleString = schemaObject.Example.ToString();
                    }
                }
                else if (!string.IsNullOrEmpty(referenceKey) && schemaDictionary[referenceKey].Example != null)
                {
                    sampleString = $"\"{schemaDictionary[referenceKey].Example}\"";
                }
                else
                {
                    var sampleValue = GenerateSampleData(schemaObject.GetDataType(), schemaObject.GetFormat(), schemaObject.Pattern, schemaObject.Properties,
                                                         schemaObject.Items, schemaObject.MinItems, schemaObject.MaxItems);
                    sampleString = sampleValue;
                }

                return(sampleString);
            }
コード例 #2
0
        private static void CheckStringType(SchemaObject propertySchemaObject, KeyValuePair <string, object> kv,
                                            TestMessageResult testMessageResult)
        {
            if (kv.Value != null)
            {
                var notGuid     = (!Guid.TryParse(kv.Value.ToString(), out _));
                var notDateTime = (!DateTime.TryParse(kv.Value.ToString(), out _));

                if (propertySchemaObject.Format != null)
                {
                    var format = propertySchemaObject.GetFormat();
                    // Validate specific string formats
                    if (format == Format.DateTime && notDateTime)
                    {
                        AddFormatError(testMessageResult, kv.Value.ToString(), kv.Key, Format.DateTime);
                    }
                }
                if (notGuid && propertySchemaObject.Reference == "Guid")
                {
                    AddFormatError(testMessageResult, kv.Value.ToString(), kv.Key, Format.Guid);
                }
            }
            else if (kv.Value == null && propertySchemaObject.Reference == "Guid")
            {
                AddFormatError(testMessageResult, null, kv.Key, Format.Guid);
            }
        }
コード例 #3
0
        private static void CheckNumberType(SchemaObject propertySchemaObject, KeyValuePair <string, object> kv,
                                            TestMessageResult testMessageResult)
        {
            var stringVal = kv.Value.ToString();
            var isDouble  = double.TryParse(stringVal, out var doubleVal) && double.IsFinite(doubleVal);
            var isFloat   = float.TryParse(stringVal, out var floatVal) && float.IsFinite(floatVal);

            // Validate number data type
            if (!isDouble && !isFloat)
            {
                AddTypeError(testMessageResult, stringVal, kv.Key, propertySchemaObject.Type);
            }

            if (propertySchemaObject.Format != null)
            {
                var format = propertySchemaObject.GetFormat();
                // Validate specific number formats
                if (format == Format.Float && !isFloat)
                {
                    AddFormatError(testMessageResult, stringVal, kv.Key, Format.Float);
                }
                else if (format == Format.Double && !isDouble)
                {
                    AddFormatError(testMessageResult, stringVal, kv.Key, Format.Double);
                }
            }
        }
コード例 #4
0
ファイル: SampleData.cs プロジェクト: AndyButland/Totem
        public static string GenerateSampleData(DataType dataType, Format format, string pattern = null, CaseInsensitiveDictionary <SchemaObject> properties = null, SchemaObject items = null, int minItems = 0, int maxItems = 0)
        {
            if (dataType == DataType.Integer)
            {
                return(GenerateInteger(format));
            }

            if (dataType == DataType.Number)
            {
                return(GenerateNumber(format));
            }

            if (dataType == DataType.Array)
            {
                return(items != null?GenerateArray(dataType, items.GetFormat(), minItems, maxItems, items.Pattern) : "[]");
            }

            if (dataType == DataType.Object)
            {
                // Replace quotes inside nested JSON objects and remove slashes
                return(GenerateObject(properties).Replace(@"\", "").Replace("\"{", "{").Replace("}\"", "}"));
            }

            // Default is to treat as a string
            return(GenerateString(format, pattern));
        }