Esempio n. 1
0
        /// <summary>
        /// Generate code to perform validation on a parameter or property
        /// </summary>
        /// <param name="type">The type to validate</param>
        /// <param name="scope">A scope provider for generating variable names as necessary</param>
        /// <param name="valueReference">A reference to the value being validated</param>
        /// <param name="isRequired">True if the parameter is required.</param>
        /// <param name="modelReference">A reference to the models array</param>
        /// <returns>The code to validate the reference of the given type</returns>
        public static string ValidateType(this IType type, IScopeProvider scope, string valueReference, bool isRequired, string modelReference = "client._models")
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            CompositeType  composite  = type as CompositeType;
            SequenceType   sequence   = type as SequenceType;
            DictionaryType dictionary = type as DictionaryType;
            PrimaryType    primary    = type as PrimaryType;
            EnumType       enumType   = type as EnumType;

            if (primary != null)
            {
                return(primary.ValidatePrimaryType(scope, valueReference, isRequired));
            }
            else if (enumType != null && enumType.Values.Any())
            {
                return(enumType.ValidateEnumType(scope, valueReference, isRequired));
            }
            else if (composite != null && composite.Properties.Any())
            {
                return(composite.ValidateCompositeType(scope, valueReference, isRequired, modelReference));
            }
            else if (sequence != null)
            {
                return(sequence.ValidateSequenceType(scope, valueReference, isRequired, modelReference));
            }
            else if (dictionary != null)
            {
                return(dictionary.ValidateDictionaryType(scope, valueReference, isRequired, modelReference));
            }

            return(null);
        }