Source/NFX/DataAccess/CRUD/Row.cs
/// <summary>
/// Validates row field using Schema.FieldDef settings.
/// This method is invoked by base Validate() implementation.
/// The method is not expected to throw exception in case of failed validation, rather return exception instance because
///  throwing exception really hampers validation performance when many rows need to be validated
/// </summary>
Validates row field using Schema.FieldDef settings.
This method is invoked by base Validate() implementation.
The method is not expected to throw exception in case of failed validation, rather return exception instance because
throwing exception really hampers validation performance when many rows need to be validated
Validates row field using Schema . FieldDef settings . This method is invoked by base Validate () implementation . The method is not expected to throw exception in case of failed validation rather return exception instance because throwing exception really hampers validation performance when many rows need to be validated
public virtual Exception ValidateField(string targetName, Schema.FieldDef fdef)
            {
                if (fdef == null)
                  throw new CRUDFieldValidationException(Schema.Name,
                                                         StringConsts.NULL_STRING,
                                                         StringConsts.ARGUMENT_ERROR + ".ValidateField(fdef=null)");
                var atr = fdef[targetName];
                if (atr==null) return null;
                var value = GetFieldValue(fdef);
                if (value==null ||
                    (value is string && ((string)value).IsNullOrWhiteSpace()) ||
                    (value is Distributed.GDID && ((Distributed.GDID)value).IsZero)
                   )
                {
                   if (atr.Required)
                    return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_REQUIRED_ERROR);
                   return null;
                }
                if (value is IValidatable)
                   return (((IValidatable)value).Validate(targetName));
                if (value is IEnumerable<IValidatable>)
                {
                   foreach(var v in (IEnumerable<IValidatable>)value)
                   {
                     if (v==null) continue;
                     var error = v.Validate(targetName);
                     if (error!=null) return error;
                   }
                   return null;
                }
                if (value is IEnumerable<KeyValuePair<string, IValidatable>>)
                {
                   foreach(var kv in (IEnumerable<KeyValuePair<string, IValidatable>>)value)
                   {
                     var v = kv.Value;
                     if (v==null) continue;
                     var error = v.Validate(targetName);
                     if (error!=null) return error;
                   }
                   return null;
                }
                if (atr.HasValueList)
                {
                    var parsed = atr.ParseValueList();
                    if (isSimpleKeyStringMap(parsed))
                    {
                      if (!parsed.ContainsKey(value.ToString()))
                          return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_IS_NOT_IN_LIST_ERROR);
                    }
                }
                if (atr.MinLength>0)
                    if (value.ToString().Length<atr.MinLength)
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MIN_LENGTH_ERROR);
                if (atr.MaxLength>0)
                    if (value.ToString().Length>atr.MaxLength)
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_MAX_LENGTH_ERROR);
                if (atr.Kind==DataKind.ScreenName)
                {
                    if (!NFX.Parsing.DataEntryUtils.CheckScreenName(value.ToString()))
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_SCREEN_NAME_ERROR);
                }
                else if (atr.Kind==DataKind.EMail)
                {
                    if (!NFX.Parsing.DataEntryUtils.CheckEMail(value.ToString()))
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_EMAIL_ERROR);
                }
                else if (atr.Kind==DataKind.Telephone)
                {
                    if (!NFX.Parsing.DataEntryUtils.CheckTelephone(value.ToString()))
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name, StringConsts.CRUD_FIELD_VALUE_PHONE_ERROR);
                }
                if (value is IComparable)
                {
                    var error = CheckMinMax(atr, fdef.Name, (IComparable)value);
                    if (error!=null) return error;
                }
                if (atr.FormatRegExp.IsNotNullOrWhiteSpace())
                {
                   var complex = atr.Metadata==null? false
                                                   : atr.Metadata
                                                        .AttrByName("validate-format-regexp-complex-types")
                                                        .ValueAsBool(false);
                   if (complex || value is string)
                   {
                     if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), atr.FormatRegExp))
                       return new CRUDFieldValidationException(Schema.Name, fdef.Name,
                         StringConsts.CRUD_FIELD_VALUE_REGEXP_ERROR.Args(atr.FormatDescription ?? "Input format: {0}".Args(atr.FormatRegExp)));
                   }
                }
                return null;
            }
public virtual Exception ValidateField ( string targetName , Schema . FieldDef fdef ) { if ( fdef == null ) throw new CRUDFieldValidationException ( Schema . Name , StringConsts . NULL_STRING , StringConsts . ARGUMENT_ERROR + " " ) ; var atr = fdef [ targetName ] ; if ( atr == null ) return null ; var value = GetFieldValue ( fdef ) ; if ( value == null || ( value is string && ( ( string ) value ) . IsNullOrWhiteSpace ( ) ) || ( value is Distributed . GDID && ( ( Distributed . GDID ) value ) . IsZero ) ) { if ( atr . Required ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_REQUIRED_ERROR ) ; return null ; } if ( value is IValidatable ) return ( ( ( IValidatable ) value ) . Validate ( targetName ) ) ; if ( value is IEnumerable < IValidatable > ) { foreach ( var v in ( IEnumerable < IValidatable > ) value ) { if ( v == null ) continue ; var error = v . Validate ( targetName ) ; if ( error != null ) return error ; } return null ; } if ( value is IEnumerable < KeyValuePair < string , IValidatable > > ) { foreach ( var kv in ( IEnumerable < KeyValuePair < string , IValidatable > > ) value ) { var v = kv . Value ; if ( v == null ) continue ; var error = v . Validate ( targetName ) ; if ( error != null ) return error ; } return null ; } if ( atr . HasValueList ) { var parsed = atr . ParseValueList ( ) ; if ( isSimpleKeyStringMap ( parsed ) ) { if ( ! parsed . ContainsKey ( value . ToString ( ) ) ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_IS_NOT_IN_LIST_ERROR ) ; } } if ( atr . MinLength > 0 ) if ( value . ToString ( ) . Length < atr . MinLength ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_MIN_LENGTH_ERROR ) ; if ( atr . MaxLength > 0 ) if ( value . ToString ( ) . Length > atr . MaxLength ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_MAX_LENGTH_ERROR ) ; if ( atr . Kind == DataKind . ScreenName ) { if ( ! NFX . Parsing . DataEntryUtils . CheckScreenName ( value . ToString ( ) ) ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_SCREEN_NAME_ERROR ) ; } else if ( atr . Kind == DataKind . EMail ) { if ( ! NFX . Parsing . DataEntryUtils . CheckEMail ( value . ToString ( ) ) ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_EMAIL_ERROR ) ; } else if ( atr . Kind == DataKind . Telephone ) { if ( ! NFX . Parsing . DataEntryUtils . CheckTelephone ( value . ToString ( ) ) ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_PHONE_ERROR ) ; } if ( value is IComparable ) { var error = CheckMinMax ( atr , fdef . Name , ( IComparable ) value ) ; if ( error != null ) return error ; } if ( atr . FormatRegExp . IsNotNullOrWhiteSpace ( ) ) { var complex = atr . Metadata == null ? false : atr . Metadata . AttrByName ( " " ) . ValueAsBool ( false ) ; if ( complex || value is string ) { if ( ! System . Text . RegularExpressions . Regex . IsMatch ( value . ToString ( ) , atr . FormatRegExp ) ) return new CRUDFieldValidationException ( Schema . Name , fdef . Name , StringConsts . CRUD_FIELD_VALUE_REGEXP_ERROR . Args ( atr . FormatDescription ?? " " . Args ( atr . FormatRegExp ) ) ) ; } } return null ; }
Validates row field using Schema.FieldDef settings.
Validates row field using Schema . FieldDef settings .
//List<IValidatable>, IValidatable[] //Dictionary<string, IValidatable> //check dictionary //For those VERY RARE cases when RegExpFormat may need to be applied to complex types, i.e. StringBuilder //set the flag in metadata to true, otherwise regexp gets matched only for STRINGS
targetName
string
fdef
Schema.FieldDef