String Schema Type
The string schema type is the simplest of the schema types supported by MVOM.
Schema Definition Properties
In addition to the base schema definition properties the string
type has the following additional properties:
Property | Type | Mandatory | Default | Description |
---|---|---|---|---|
type | "string" | ✔️ | The type literal for a string schema type | |
enum | string[] | If specified, value will be validated against this list of allowed values | ||
match | RegExp | If specified, value will be matched against this regular expression | ||
foreignKey | object | If specified, value will be validated for foreign key constraints. See link for details on format. |
Formatting
A string schema type will not be formatted. The input and output values will remain the same.
Database Value | JavaScript Value |
---|---|
foo | foo |
Validating enumerations
The enum
property allows you to specify an array of permitted values for string types. If the value of the property is not in this list an error will be thrown when saving.
Validating pattern matching
The match
property allows you to specify a regular expression which will validate the value to determine if there is a match of the regular expression. If the value of the property does not match the regular expression an error will be thrown when saving.
Validating foreign keys
The foreignKey
property allows for validation to ensure that the value is a foreign key to a record of one or more file(s). If the value of the property is not a foreign key to at least one of the specified files then an error will be thrown when saving.
Properties of foreign key validator
Property | Type | Mandatory | Description |
---|---|---|---|
file | string | string[] | ✔️ | The name of the file(s) in the MultiValue database to validate against. Foreign key validation will pass if the value is an id to any of the listed files. |
entityName | string | ✔️ | A friendly name for the foreign entity being validated Used for validation errors only |
keysToIgnore | string[] | A list of keys which will not be validated |
Example
Consider a value that is intended to be a foreign key to a file named ITEM
. The structure of the schema definition would be as follows:
const schemaDefinition = {
stringWithForeignKeyProperty: {
type: 'string',
path: 1,
dictionary: 'STRING_DICT',
required: true,
foreignKey: { file: 'ITEM', entityName: 'item' },
},
};
const schema = new Schema(schemaDefinition);
Delimited string foreign key validation
Foreign key validation can also be run against the parts of a delimited string. For instance, if you had a string formatted as {part1}*{part2}
(i.e. 2 parts with an asterisk delimiter), you can validate one or both parts as separate foreign key validations. In this case, the foreign key validation format is as follows:
Property | Type | Mandatory | Description |
---|---|---|---|
[key: number] | Foreign Key Validator Definition | ✔️ | One or more dynamic integer key values representing the 0-indexed position in the string after being split by the delimiter The property value's type is the standard foreign key validation object. Only those parts of the string which are to be validated require a definition (i.e. the undefined parts will be ignored) |
splitCharacter | string | ✔️ | The string's delimiter character |
Example
Consider a string of the format {itemId}*{locationId}
which is a string delimited by an asterisk where the first part of the string is a foreign key to a file named ITEM
and the second part of the string is a foreign key to a file named LOCATION
. The structure of the schema definition would appear as follows:
const schemaDefinition = {
compoundStringWithForeignKeyProperty: {
type: 'string',
path: 1,
dictionary: 'STRING_DICT',
required: true,
foreignKey: {
0: { file: 'ITEM', entityName: 'item' },
1: { file: 'LOCATION', entityName: 'location' },
splitCharacter: '*',
},
},
};
const schema = new Schema(schemaDefinition);
Example
const schemaDefinition = {
stringProperty: {
type: 'string',
path: 1,
dictionary: 'STRING_DICT',
required: true,
enum: ['foo', 'bar', 'baz'],
},
};
const schema = new Schema(schemaDefinition);