Nullable
Nullable validator are used to represent a value that may or may not be present.
How it works
The nullable validator is a wrapper around another type. It allows the value to be null
in addition to the normal values of the wrapped type.
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = string().nullable();
final result = schema.tryParse(null);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
In this example, the schema is a nullable string. The tryParse
method is called with null
as the argument. Since the schema is nullable, the result is successful.
TIP
The AcanthisParseResult object, that is returned by the parse
and tryParse
method is described here.
Default value
You can also specify a default value for the nullable type.
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = string().nullable(defaultValue: 'default');
final result = schema.tryParse(null);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
assert(result.value == 'default');
}
And for Maps or Lists?
The nullable type can be used with maps and lists as well.
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = object({
'name': string().min(3),
'age': number().positive(),
}).nullable();
final result = schema.tryParse(null);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = string().list().nullable();
final result = schema.tryParse(null);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
Also you can create lists of nullable types.
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = string().nullable().list();
final result = schema.tryParse(null);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
And maps with nullable types.
dart
import 'package:acanthis/acanthis.dart';
void main() {
final schema = object({
'name': string().min(3).nullable(),
'age': number().positive(),
});
final result = schema.tryParse({
'name': null,
'age': 25,
});
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}