ValidateOptions (open)

Validate the options passed to a function

Syntax

// Core function, no LOADLIB necessary

RECORD FUNCTION ValidateOptions(RECORD defaultoptions, RECORD options, RECORD metaoptions)

Parameters

RECORD defaultoptions

Defaults, and structure which the options should follow

RECORD options

Passed in options

RECORD metaoptions

Meta options describing the otpions

discard

List of cells to discard if passed

enums

Record with list of allowed string values for options

notypecheck

List of cells whose type does not have to be checked

optional

List of cells which will only exist in the output if they're explicitly set

passthrough

Pass through any unrecognized options in the output record, instead of failing on them

passthroughin

Store any unrecognized options in the specified cell, instead of failing on them

required

List of cells which must exist in the options record

title

Name for the options record used in exceptions after the word 'in'. Defaults to 'options'

Return value

RECORD

Validated options, with missing values added

Examples

// Throws if options doesn't contain a cell 'A'.
options := ValidateOptions([ a := 1 ], options, [ required := [ "a" ] ]);

// Returns a record without cell 'A' if options doesn't contain that cell
options := ValidateOptions([ a := 1 ], options, [ optional := [ "a" ] ]);

// Removes cell 'A' from the options
options := ValidateOptions([ a := 1, b := 1 ], options, [ discard := [ "a" ] ]);

// Allow all types for cell 'A' (eg. 0m, "test", DEFAULT OBJECT)
options := ValidateOptions([ a := 1 ], options, [ notypecheck := [ "a" ] ]);

// Restricts cell 'ELEMENT' to the values 'earth', 'water', 'fire' and 'wind'
options := ValidateOptions(
    [ element := "wind"
    ], options,
    [ enums :=    [ element := [ "earth", "water", "fire", "wind" ] ]
    ]);