Validation

Validation Data and Input

MaSH includes a fully-featured validation library with over 150 rules for different data types such as postal/zip codes, dates and times, credit card numbers, email addresses, urls and many more. You can use the mash.validator object to check any data accessible to your mashlets.

Under the hood MaSH uses the Respect Validation package. You can read more about the package here.


Validating Data

Below are some examples of validating data in MaSH. For a complete list of validation rules click here.

Natural

# Validate URLs
printline "Validate URLs"
printline "============="
set input to "https://foldr.io"
set valid to mash.validator.url().validate(input)
printline "{{ input }} validates {{ valid }}"

set input to "://foldr.io"
set valid to mash.validator.url().validate("://foldr.io")
printline "{{ input }} validates {{ valid }}"

printline

# Validate GB Postcodes
printline "Validate GB Postcodes"
printline "====================="
set input to "BS16 7FR"
set valid to mash.validator.postalCode("GB").validate(input)
printline "{{ input }} validates {{ valid }}"

set input to "NNP24 46HA"
set valid to mash.validator.postalCode("GB").validate(input)
printline "{{ input }} validates {{ valid }}"

# GB Postal codes are only considered valid if they contain a space.
# This block is used to format them appropriately
block formatPostcode code
    set code to str(code).replaceMatches("/\s/", '')
    set code to code.replaceMatches("/[^A-Za-z0-9]/", '')
    set code to code.upper()
    return "{{ code.slice(0, -3) }} {{ code.slice(-3) }}"
end

# Without a space this will be considered invalid
set input to "BS167FR"
set valid to mash.validator.postalCode("GB").validate(input)
printline "{{ input }} validates {{ valid }}"

# Use our block to format appropriately
set input to "BS167FR"
set formatted to formatPostcode(input)
set valid to mash.validator.postalCode("GB").validate(formatted)
printline "{{ input }} formatted to {{ formatted }} validates {{ valid }}"

printline

# Validate US Zipcodes
printline "Validate US Zipcodes"
printline "====================="
set input to "35801"
set valid to mash.validator.postalCode("US").validate(input)
printline "{{ input }} validates {{ valid }}"

set input to "358179"
set valid to mash.validator.postalCode("US").validate(input)
printline "{{ input }} validates {{ valid }}"

Standard

# Validate URLs
printline("Validate URLs")
printline("=============")
input = "https://foldr.io"
valid = mash.validator.url().validate(input)
printline("{{ input }} validates {{ valid }}")

input = "://foldr.io"
valid = mash.validator.url().validate("://foldr.io")
printline("{{ input }} validates {{ valid }}")

printline()

# Validate GB Postcodes
printline("Validate GB Postcodes")
printline("=====================")
input = "BS16 7FR"
valid = mash.validator.postalCode("GB").validate(input)
printline("{{ input }} validates {{ valid }}")

input = "NNP24 46HA"
valid = mash.validator.postalCode("GB").validate(input)
printline("{{ input }} validates {{ valid }}")

# GB Postal codes are only considered valid if they contain a space.
# This block is used to format them appropriately
block formatPostcode(code) {
    code = str(code).replaceMatches("/\s/", '')
    code = code.replaceMatches("/[^A-Za-z0-9]/", '')
    code = code.upper()
    return "{{ code.slice(0, -3) }} {{ code.slice(-3) }}"
}

# Without a space this will be considered invalid
input = "BS167FR"
valid = mash.validator.postalCode("GB").validate(input)
printline("{{ input }} validates {{ valid }}")

# Use our block to format appropriately
input = "BS167FR"
formatted = formatPostcode(input)
valid = mash.validator.postalCode("GB").validate(formatted)
printline("{{ input }} formatted to {{ formatted }} validates {{ valid }}")

printline()

# Validate US Zipcodes
printline("Validate US Zipcodes")
printline("=====================")
input = "35801"
valid = mash.validator.postalCode("US").validate(input)
printline("{{ input }} validates {{ valid }}")

input = "358179"
valid = mash.validator.postalCode("US").validate(input)
printline("{{ input }} validates {{ valid }}")

Output

Validate URLs
=============
https://foldr.io validates true
://foldr.io validates false

Validate GB Postcodes
=====================
BS16 7FR validates true
NNP24 46HA validates false
BS167FR validates false
BS167FR formatted to BS16 7FR validates true

Validate US Zipcodes
=====================
35801 validates true
358179 validates false

← All articles