Exemplo n.º 1
0
            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);
            }
Exemplo n.º 2
0
        private void ChecksForMessage(SchemaObject propertySchemaObject, KeyValuePair <string, object> kv,
                                      TestMessageResult testMessageResult, bool allowSubset)
        {
            var dataType = propertySchemaObject.GetDataType();

            if (dataType == DataType.Integer)
            {
                CheckIntegerType(propertySchemaObject, kv, testMessageResult);
            }

            if (dataType == DataType.Number)
            {
                CheckNumberType(propertySchemaObject, kv, testMessageResult);
            }

            if (dataType == DataType.Boolean)
            {
                CheckBooleanType(propertySchemaObject, kv, testMessageResult);
            }

            if (dataType == DataType.String)
            {
                CheckStringType(propertySchemaObject, kv, testMessageResult);
            }

            if (dataType == DataType.Array)
            {
                CheckArrayType(propertySchemaObject, kv, testMessageResult);
            }

            if (dataType == DataType.Object)
            {
                CheckObjectType(propertySchemaObject, kv, testMessageResult, allowSubset);
            }
        }