示例#1
0
        public ReturnTypeModel(JsonSchema parameter, TypeResolverBase resolver)
        {
            _parameter = parameter;
            _resolver  = resolver;

            Type = _resolver.Resolve(_parameter.ActualTypeSchema, _parameter.IsNullable(SchemaType.JsonSchema), null);
        }
        private void ValidateEnum(JToken token, JsonSchema schema, SchemaType schemaType, string propertyName, string propertyPath, List <ValidationError> errors)
        {
            if (schema.IsNullable(schemaType) && token?.Type == JTokenType.Null)
            {
                return;
            }

            if (schema.Enumeration.Count > 0 && schema.Enumeration.All(v => v?.ToString() != token?.ToString()))
            {
                errors.Add(new ValidationError(ValidationErrorKind.NotInEnumeration, propertyName, propertyPath, token, schema));
            }
        }
        private void ValidateType(JToken token, JsonSchema schema, SchemaType schemaType, string propertyName, string propertyPath, List <ValidationError> errors)
        {
            if (token.Type == JTokenType.Null && schema.IsNullable(schemaType))
            {
                return;
            }

            var types = GetTypes(schema).ToDictionary(t => t, t => (ICollection <ValidationError>) new List <ValidationError>());

            if (types.Count > 1)
            {
                foreach (var type in types)
                {
                    ValidateArray(token, schema, schemaType, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateString(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateNumber(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateInteger(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateBoolean(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateNull(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                    ValidateObject(token, schema, type.Key, propertyName, propertyPath, (List <ValidationError>)type.Value);
                }

                // just one has to validate when multiple types are defined
                if (types.All(t => t.Value.Count > 0))
                {
                    errors.Add(new MultiTypeValidationError(
                                   ValidationErrorKind.NoTypeValidates, propertyName, propertyPath, types, token, schema));
                }
            }
            else
            {
                ValidateArray(token, schema, schemaType, schema.Type, propertyName, propertyPath, errors);
                ValidateString(token, schema, schema.Type, propertyName, propertyPath, errors);
                ValidateNumber(token, schema, schema.Type, propertyName, propertyPath, errors);
                ValidateInteger(token, schema, schema.Type, propertyName, propertyPath, errors);
                ValidateBoolean(token, schema, schema.Type, propertyName, propertyPath, errors);
                ValidateNull(token, schema, schema.Type, propertyName, propertyPath, errors);
                ValidateObject(token, schema, schema.Type, propertyName, propertyPath, errors);
            }
        }