void Start() { startingPosition = transform.localPosition; inventory = GameObject.FindObjectOfType <Inventory>(); validateObject = GameObject.FindObjectOfType <ValidateObject> (); displayCanvas = FindObjectOfType <DisplayCanvas> (); }
public bool IsValid(IList values) { Errors = new List <string>(); if (new IsEmpty().IsValid(values)) { if (IgnoreEmpty) { return(true); } else { Errors.Add("can't be null object"); return(false); } } bool isValid = true; foreach (object value in values) { ValidateObject validator = new ValidateObject(IgnoreEmpty); if (!validator.IsValid(value)) { Errors.AddRange(validator.Errors); isValid = false; } } return(isValid); }
public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObject objectCreate) { if (objectCheck == null) { throw new ArgumentNullException("objectCheck"); } this.objectCheck = objectCheck; this.NumberOfInvalidObjects = allocation; this.objects = new T[allocation]; ConstructorInfo constructor = typeof(T).GetConstructor(new Type[0]); for (int i = 0; i < allocation; i++) { if (objectCreate != null) { T t = objectCreate(); if (t == null) { throw new Exception("CreateNewObject delegate cannot return null."); } this.objects[i] = t; } else { if (constructor == null) { throw new Exception(string.Format("CreateNewObject delegate must be specified or T ({0}) must implement a parameterless constructor.", typeof(T))); } this.objects[i] = (T)constructor.Invoke((object[])null); } } }
static void Main(string[] args) { ValidateObject Nombre = new ValidateObject() { Value = "Ped" }; ValidateObject Edad = new ValidateObject() { Value = 35 }; Nombre.Rules.Add(new StringRule()); Nombre.Rules.Add(new LengthRule()); Edad.Rules.Add(new IntRule()); if (Nombre.ValidateField()) { Console.WriteLine("Campo Correcto"); } else { Console.WriteLine("Campo Fallido"); } }
/// <summary> /// Throws if invalid. /// </summary> public void Validate() { if (ValidateObject != null) { var args = new ValidateObjectEventArgs(this); ValidateObject.Raise(this, args); } }
void Start() { startingPosition = transform.localPosition; inventory = GameObject.FindObjectOfType <Inventory>(); player = GameObject.Find("Player"); validateObject = GameObject.FindObjectOfType <ValidateObject> (); objectCount = Random.Range(1, maxObjectCount + 1); }
public LoginViewModel(ISettingService settingService) { _settingService = settingService; _userName = new ValidateObject <string>(); _password = new ValidateObject <string>(); AddValidations(); }
public ResourcePool(int allocation, ValidateObject objectCheck) : this(allocation, objectCheck, null) { }
public IParseResult Parse(IEnumerator <string> args) { bool first = true; TClass parsedClass = new(); List <Error> errors = new(); HashSet <IOption <TClass> > optionsRemaining = new(allOptions); HashSet <ISwitch <TClass> > switchesRemaining = new(allSwitches); List <string> multiValuesFound = new(); int valuesFound = 0; while (args.MoveNext()) { string a = args.Current; // If the user asks for help, immediately stop parsing if (config.StringComparer.Equals(a, config.ShortHelpSwitch) || config.StringComparer.Equals(a, config.LongHelpSwitch)) { errors.Add(new Error(ErrorCode.HelpRequested, string.Empty)); return(new FailedParseWithVerb <TClass>(this, errors)); } // It is POSSIBLE for the user to set up a verb with arguments that also has a sub-verb as much as I hate that idea // So only allow the first argument to be the verb. It should be interpreted as a value or whatever if found elsewhere. // If it happens to be a verb, we just hand over the parsing if (first && verbsByName.TryGetValue(a, out IVerb? verb)) { return(verb.Parse(args)); } if (allOptionsByName.TryGetValue(a, out IOption <TClass>?oval)) { if (args.MoveNext()) { a = args.Current; // The option might only have a short or long name. However if both return false, that means we already saw it. if (optionsRemaining.Remove(oval)) { Error error = oval.SetValue(parsedClass, a); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } else { errors.Add(new Error(ErrorCode.DuplicateOption, "An Option appeared twice: " + a)); } } else { errors.Add(new Error(ErrorCode.OptionMissingValue, "An Option was missing a value: " + a)); } } else if (allSwitchesByName.TryGetValue(a, out ISwitch <TClass>?sval)) { // The switch might only have a short or long name. However if both return false, that means we already saw it. if (switchesRemaining.Remove(sval)) { Error error = sval.SetValue(parsedClass, string.Empty); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } else { errors.Add(new Error(ErrorCode.DuplicateSwitch, "A Switch appeared twice: " + a)); } } // Might be a Value else if (valuesFound < AllValues.Count) { Error error = AllValues[valuesFound++].SetValue(parsedClass, a); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } // Might be a MultiValue, unless it starts with something that should be ignored else if (MultiValue != null) // && !MultiValue.HasIgnoredPrefix(arg)) { multiValuesFound.Add(a); } // It's something unrecognized else { errors.Add(new Error(ErrorCode.UnexpectedArgument, "Found an unexpected argument: " + a)); } first = false; } // Set all remaining values to default for (int i = valuesFound; i < AllValues.Count; i++) { Error error = AllValues[i].SetValue(parsedClass, null); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } bool gotMultiValue = false; // Set the MultiValue if (MultiValue != null) { Error error = MultiValue.SetValue(parsedClass, multiValuesFound); gotMultiValue = true; if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } // Set all remaining options and switches to their default values foreach (IOption <TClass> opt in optionsRemaining) { Error error = opt.SetValue(parsedClass, null); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } foreach (ISwitch <TClass> sw in switchesRemaining) { Error error = sw.SetValue(parsedClass, null); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } // Make sure all of the stuff we've set so far is good, if not then bail out if (errors.Count > 0) { return(new FailedParseWithVerb <TClass>(this, errors)); } // Evaluate dependencies; we know we got the value if it doesn't appear in our lists of remaining stuff foreach (IOption <TClass> opt in allOptions) { Error error = opt.EvaluateDependencies(parsedClass, !optionsRemaining.Contains(opt)); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } foreach (ISwitch <TClass> sw in allSwitches) { Error error = sw.EvaluateDependencies(parsedClass, !switchesRemaining.Contains(sw)); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } // As for values, we can use the valuesFound variable for (int i = 0; i < allValues.Count; i++) { IValue <TClass> val = allValues[i]; Error error = val.EvaluateDependencies(parsedClass, valuesFound > i); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } if (MultiValue != null) { Error error = MultiValue.EvaluateDependencies(parsedClass, gotMultiValue); if (error.ErrorCode != ErrorCode.Ok) { errors.Add(error); } } if (errors.Count > 0) { return(new FailedParseWithVerb <TClass>(this, errors)); } string?errMsg = ValidateObject?.Invoke(parsedClass); if (string.IsNullOrEmpty(errMsg)) { return(new SuccessfulParse <TClass>(this, parsedClass)); } else { errors.Add(new Error(ErrorCode.ObjectFailedValidation, errMsg)); return(new FailedParseWithVerb <TClass>(this, errors)); } }