Beispiel #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 IModelType type, IChild scope, string valueReference, bool isRequired, string modelReference = "client.models")
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(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())
            {
                if (enumType.ModelAsString)
                {
                    return(New <PrimaryType>(KnownPrimaryType.String).ValidatePrimaryType(scope, valueReference, isRequired));
                }
                return(enumType.ValidateEnumType(scope, valueReference, isRequired));
            }
            else if (composite != null && composite.Properties.Any())
            {
                return(ValidateCompositeType(scope, valueReference, isRequired));
            }
            else if (sequence != null)
            {
                return(sequence.ValidateSequenceType(scope, valueReference, isRequired, modelReference));
            }
            else if (dictionary != null)
            {
                return(dictionary.ValidateDictionaryType(scope, valueReference, isRequired, modelReference));
            }

            return(null);
        }