Script Library: 1238 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 
View scriptLicenseDownload documentation as: HTML or editable
Download scriptHistoryOther scripts by: chrisrg

Documentation for: filtered-import.r


Import and Validate User Input

A Brief Guide

Christopher Ross-Gill
30 April, 2007

1. Introduction

This script allows you to Import and Validate user input.

2. Basic Usage

%filtered-import.r defines two global functions: 'import and 'as. This documentation focusses on 'import.

2.1 Import

Import takes user input in the form [name value name value ...] where name is of word! type:

    user-data: [name "Chris" age "Not telling" start-date "5 May 2007"]

Import provides rules for which values you want, and how they must conform:

    personal-data-rule: [
        name: string!
    ]

This is a very basic rule that will select the name value and ensure that value is a valid string.

    >> import user-data personal-data-rule
    == [name "Chris"]

3. Validation

3.1 Implicit

Import will fail if any value does not meet the specification:

    name: string!
    age: integer!

3.2 Format

You can specify the format of the value, including the use of predifined charsets:

    name: string! [chars-ua some chars la]

3.3 Optional

You can make a value optional:

    name: opt string!

In this case, if the 'name value is empty or not present, 'name in the result block will be set to none!. Otherwise the 'name value must pass the validation rules.

3.4 Acceptance

    terms-of-service: logic! is accepted

3.5 Comparison

You can provide some parameters that any value must conform to:

    name: string! is not within ["Carl"]
    age: integer! is between [18 65]
    start-date: date! is after 30-April-2007

3.6 Length Comparison

You can also provide parameters to the length of a value:

    name: string!
    length is more-than 3

3.7 Confirmation

You can confirm two values match:

    address: email! is confirmed by :address-confirm

4. On Failure

4.1 Why did it fail?

As standard, import will return none! if validation fails. You can optionally pass a word that will be set with a list of validation failure notices:

    import/else user-data personal-data-rule 'errors

4.2 Custom notices

You can define a notice for each individual validation rule:

    name: string! or "Need a name here!"
    is not within ["Carl"] or "You can't be Carl!"
    length is between [2 50] or "Your name doesn't fit"