Skip to main content

ConfigurationSchema API

Overview

The ConfigurationSchema class defines the configurable fields for your application.

Constructor

new ConfigurationSchema(options)

ParamTypeDescription
[options]objectOptional settings (described below)
[options.condition]functionoptional condition to check before processing anything associated with this schema
[options.linkedParentFieldName]stringA field in the parent schema that must be set for this schema to be processed
[options.linkedParentFieldValue]stringValue that linkedParentFieldName must be set to in the parent

The ConfigurationSchema is likely to be app-specific, but it may be useful to create reusable subclasses of Types and/or Validators that can be imported by multiple applications using Configurator.

When a condition function is provided, it should have the signature

function condition(field:FieldOptions, value: any, configuration: object): boolean {/*...*/}

If the condition function returns a falsey value, all assignments to fields in this schema will be skipped.

Methods

field(name, options:FieldOptions):ConfigurationSchema

Define a new configurable field in this schema. (Returns the schema for method chaining.)

ParamTypeDescription
namestringField name (required)
[options]objectField options (described below)
[options.type]stringType name that will be used for value resolution; defaults to "string"
[options.validator]function|string|RegEx|objectAdditional validations to run after type resolution
[options.condition]functionPer-field conditional evaluation (overrides schema condition)
[options.required]booleanIf true, an error will be thrown if no value is set
[options.default]anyDefault value to assign if nothing else overrides (see SchemaDefaultsSource)
[options.allowEmpty]booleanWhether or not an empty value allowed (arrays or strings)
[options.flagHint]stringRequest a specific command-line single-character flag
[options.description]stringUsed to display help text for the field
[options.advanced]booleanUsed to filter some fields out of default help text
[options.hidden]booleanUsed to completely suppress some fields from the help text

The type name is used to look up a type resolver in the TypeRegistry, which is then used to convert an input request value into a proper output value.

See the documentation on Types and Validators for more information on how these options should be used.

A few of the field options are used directly by ConfigurationSchema or Configurator, but the field options object is deliberately kept open for extra properties to allow private contracts between field definitions and individual ConfigurationSource implementations. For example, the last four options (flagHint, description, advanced, and hidden) are defined here for reference, as they're very commonly used, but are actually only consumed by CommandLineSource.

getFields(): Map<string,FieldOptions>

Return all fields defined in this schema.

child(name, options):ConfigurationSchema

child(name, ConfigurationSchema):ConfigurationSchema

Create a new child ConfigurationSchema, either by cloning the provided schema, or creating a new one from the options passed. The newly created schema is returned for method chaining. Normal ConfiguratorSchema options are permitted.

ParamTypeDescription
namestringChild schema property name; will be used in final object hierarchy
[options]objectOptions to pass to child schema constructor (see above)

getChildren(): Map<string,ConfigurationSchema>

Return all child schemas.

getAllFieldPaths(): Map<string,FieldOptions>

Return a flattened view of all configured fields in the schema hierarchy, with the Map keys as dotted "field paths". This representation is used by the processAssignments method.

async processAssignments(fpaList, options:object):object

Process field-path assignment maps generated from sequence of ConfigurationSource implementations. Generates a candidate configuration object, then calls validate on it.

Returns the validated object, or throws if there are errors.

This method is public, but not generally used unless you are extending Configurator.

ParamTypeDescription
fpaListArray<Map<string,any>>list of field-path assignment maps, in source sequence order
[options]objectassignment options
[options.strict]booleanstrict validation (default: true)
[options.types]Typesoverride types
[options.configuration]objectinitial configuration object

async validate(inputConfig:object, options:object):object

Validate a candidate configuration object against the schema.

Returns the validated object, or throws if there are errors.

ParamTypeDescription
inputConfigobjectobject to validate against the schema
[options]objectvalidation options, described below
[options.types]Typespass in Types registry to do a typecheck pass during validation
[options.validators]Validatorsallows you to override schema's Validators registry
[options.strict]booleanenable strict validation
[options.populateDefaults]booleanenable fallback defaults mode

You can omit passing types if you have already typechecked the assignments.

The strict option will throw an error if unknown fields exist in the object, or if any types are unknown.

The populateDefaults option provides a fallback method to setting fields in implementations where the DefaultsSource was not used.

This method is public, but not generally used unless you are extending Configurator.

Accessors

AccessorTypeDescription
fieldsMap<string,FieldOptions>Fields defined in the schema (getFields() clones)
childrenArray<ConfigurationSchema>Children defined in the schema (`getChildren() clones)
parent[ConfigurationSchema]Parent schema, if this schema is a child
pathstringDotted path to this schema from the root