Article
· Mar 17, 2017 2m read

String Datatype with regular expression validation

Hi folks,

I've created a datatype class whoch extends from %Library.String with a REGEX parameter and the validation of the value against the regular expression. The class does not support Populate using the regular expression as a template for data population.

Code:

/// A string datatype definition which extends <class>%Library.String</class> with additional regex pattern validation. <br>
/// It does not support data population using the regex yet
Class MasterLib.DataTypes.StringRgx Extends %Library.String
{

/// Set PATTERN to empty and final, as it is not relevant on
/// this type, but is inherited from <class>%Library.String</class>
Parameter PATTERN [ Final ];

/// Set VALUELIST to empty and final, as it is not relevant on
/// this type, but is inherited from <class>%Library.String</class>
Parameter VALUELIST [ Final ];

/// Set DISPLAYLIST to empty and final, as it is not relevant on
/// this type, but is inherited from <class>%Library.String</class>
Parameter DISPLAYLIST [ Final ];

/// Set a valid regex pattern for value validation
Parameter REGEX As %String;

/// The XMLPATTERN to regex by default. Can be overridden.
Parameter XMLPATTERN = {..#REGEX};

/// Tests if the logical value <var>%val</var>, which is a string, is valid.
/// The validation is based on the class parameter settings used for the class attribute this data type is associated with.
/// In this case, <a href="#MINLEN">MINLEN</a>, <a href="#MAXLEN">MAXLEN</a>, and <a href="#REGEX">REGEX</a>.
ClassMethod IsValid(%val As %CacheString) As %Status [ ServerOnly = 0 ]
{
    // Length checks
    if (..#MAXLEN '= "") {
        if ($length(%val) > ..#MAXLEN) {
            return $$$ERROR($$$DTMaxLen, %val, ..#MAXLEN)
        }
    }
    if (..#MINLEN '= "") {
        if ($length(%val) < ..#MINLEN) {
            return $$$ERROR($$$DTMinLen, %val, ..#MINLEN)
        }
    }
    // Regex check
    if (..#REGEX '= "") {
        try {
            if '$MATCH(%val, ..#REGEX) {
                return $$$ERROR($$$DTPattern, %val, ..#REGEX)
            }
        } catch Ex {
            return $$$ERROR($$$URegexRuleSyntax)
        }
    }
    return $$$OK
}

}
Discussion (6)2
Log in or sign up to continue

Can this be used in the System/Security Management/System-side Security Paramters "password pattern" at all?  I'm trying to find a way to match a user's password to something much more restrictive than "8 to 32 alpha, upper, lower, numeric, OR punctuation". I'd like to require at least one of a few kinds of characters (1 upper, 1 punctuation).  

 

I have a nice regex for this, but I'm not sure if I can use a regex, or this cool datatype, in the above mentioned System setting.  Any recommendations?

You can set PasswordValidationRoutine in  the Security.System class. When a user is created in the Cache security database, or a user changes their password, the specified routine is called to validate the password. A tag reference may also be included in the property. The routine should be provided by the user, and must exist in the %SYS namespace (it may be mapped to a different database however.) The routine will take 2 parameters, a Username and new password, and should return a %Status code of $$$OK for successful password validation, or an error code formatted into a %Status variable. The username passed into the function will be in all lowercase, and will contain the domain name if multiple domains are enabled for the system.