View script | License | Download documentation as: HTML or editable |
Download script | History | Other scripts by: chrisrg |
[0.051] 12.873k
Documentation for: filtered-import.rImport and Validate User InputA Brief Guide Christopher Ross-Gill 30 April, 2007 1. IntroductionThis 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 ImportImport 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. Validation3.1 ImplicitImport will fail if any value does not meet the specification: name: string! age: integer! 3.2 FormatYou can specify the format of the value, including the use of predifined charsets: name: string! [chars-ua some chars la] 3.3 OptionalYou 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 Acceptanceterms-of-service: logic! is accepted 3.5 ComparisonYou 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 ComparisonYou can also provide parameters to the length of a value: name: string! length is more-than 3 3.7 ConfirmationYou can confirm two values match: address: email! is confirmed by :address-confirm 4. On Failure4.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 noticesYou 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" |