# Interface: StringTypeOptions

Schema String type options.

field enum defines a list of allowed values.

field auto that will generate the initial value of the field. It allows the value 'uuid' or a function. It cannot be used combined with default.

field lowercase that will generate the lowercase value of the field. It cannot be used combined with uppercase.

field uppercase that will generate the uppercase value of the field. It cannot be used combined with lowercase.

field trim that will removes leading and trailing whitespace from the value.

field minLength numeric value that will add a validator that ensures the given string's length is at least the given number

field maxLength numeric value that will add a validator that ensures the given string's length is at most the given number

Note

Next examples would throw the following error:

BuildSchemaError: 'lowercase' and 'uppercase' options cannot be used at the same time within property 'name' definition.

//...
const schema = new Schema({ name: {
   type: String,
   uppercase: true,
   lowercase: true
} })

//...
const stringType = new StringType('name', { uppercase: true, lowercase: true });

1
2
3
4
5
6
7
8
9
10

# Hierarchy

# Properties

# Optional auto

auto? : undefined | string


# Optional default

default? : unknown

Inherited from CoreTypeOptions.default


# Optional enum

enum? : string[] | FunctionsString


# Optional immutable

immutable? : undefined | false | true

Inherited from CoreTypeOptions.immutable

If truthy, Ottoman will disallow changes to this path once the document is saved to the database for the first time.


# Optional lowercase

lowercase? : undefined | false | true

example

// using `lowercase` with StringType.cast
//...
const element = new StringType('name', { lowercase: true });
const result = element.cast('LOWER');
console.log(result); // lower
//...

// using `lowercase` with Schema
//...
const schema = new Schema({ email: { type: String, lowercase: true } });
const User = model('User', schema);
const user = await User.create({ email: 'Dummy.ExamPle@Email.CoM' });
console.log(user.email); // dummy.example@email.com
//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Optional maxLength

maxLength? : undefined | number

example

//...
const element = new StringType('value', { maxLength: 5 });
element.validate('eleven');

// ValidationError: Property 'value' is longer than the maximum allowed length '5'

//This is another example with schema
const UserSchema = new Schema({
  name: { type: String, maxLength: 4 },
});
const User = model('User', UserSchema);
const user = await User.create({
  name: 'John Doe'
});

// ValidationError: Property 'name' is longer than the maximum allowed length '4'

//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Optional minLength

minLength? : undefined | number

example

//...
const element = new StringType('value', { minLength: 5 });
element.validate('four');

// ValidationError: Property 'value' is shorter than the minimum allowed length '5'

//This is another example with schema
const UserSchema = new Schema({
  name: { type: String, minLength: 4 },
});
const User = model('User', UserSchema);
const user = await User.create({
  name: 'Doe'
});

// ValidationError: Property 'name' is shorter than the minimum allowed length '4'
//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Optional required

required? : boolean | RequiredOption | RequiredFunction

Inherited from CoreTypeOptions.required


# Optional trim

trim? : undefined | false | true

example

// using `trim` with StringType.cast
//...
const element = new StringType('value', { trim: true });
const result = element.cast(' some text ');
console.log(result); // some text (without leading and trailing whitespace )
//...

// using `trim` with Schema
//...
const schema = new Schema({ name: { type: String, trim: true } });
const User = model('User', schema);
const user = await User.create({ name: ' John Poe ' });
console.log(user.email); // John Poe (without leading and trailing whitespace )

//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Optional uppercase

uppercase? : undefined | false | true

example

// using `uppercase` with StringType.cast
//...
const element = new StringType('code', { uppercase: true });
const result = element.cast('upper');
console.log(result); // UPPER
//...

// using `uppercase` with Schema
//...
const schema = new Schema({ code: { type: String, uppercase: true } });
const Card = model('Card', schema);
const card = await Card.create({ code: 'upper' });
console.log(card.code); // UPPER
//...
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Optional validator

validator? : ValidatorOption | ValidatorFunction | string

Inherited from CoreTypeOptions.validator