String
The string method is used to validate a string.
You can also use the class AcanthisString
to create a string schema.
Example
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().min(3);
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
TIP
The AcanthisParseResult object is described here.
Methods
email
The email method is used to validate an email.
import 'package:acanthis/acanthis.dart';
void main() {
final email = string().email();
final result = email.tryParse('test@test.com');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
INFO
The email method uses the email_validator
package to validate the email.
min
The min method is used to validate the minimum length of a string.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().min(3);
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
max
The max method is used to validate the maximum length of a string.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().max(3);
final result = name.tryParse('Aca');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
length
The length method is used to validate the exact length of a string.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().length(3);
final result = name.tryParse('Aca');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
pattern
The pattern method is used to validate a string using a regular expression.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().pattern(RegExp(r'^[a-zA-Z]+$'));
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
required
The required method is used to validate that a string is not empty.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().required();
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
contains
The contains method is used to validate that a string contains a substring.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().contains('this');
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
startsWith
The startsWith method is used to validate that a string starts with a substring.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().startsWith('Aca');
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
endsWith
The endsWith method is used to validate that a string ends with a substring.
import 'package:acanthis/acanthis.dart';
void main() {
final name = string().endsWith('is');
final result = name.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
cuid
The cuid method is used to validate a CUID.
import 'package:acanthis/acanthis.dart';
void main() {
final cuid = string().cuid();
final result = cuid.tryParse('cjbh6z5p30000j5h6v7v6v6v6v');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
cuid2
The cuid2 method is used to validate a CUID.
import 'package:acanthis/acanthis.dart';
void main() {
final cuid2 = string().cuid2();
final result = cuid2.tryParse('cjbh6z5p30000j5h6v7v6v6v6v');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
uuid
The uuid method is used to validate a UUID.
import 'package:acanthis/acanthis.dart';
void main() {
final uuid = string().uuid();
final result = uuid.tryParse('550e8400-e29b-41d4-a716-446655440000');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
nanoid
The nanoid method is used to validate a nanoid.
import 'package:acanthis/acanthis.dart';
void main() {
final nanoid = string().nanoid();
final result = nanoid.tryParse('V1StGXR8_Z5jdHi6B-myT');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
jwt
The jwt method is used to validate a JWT.
import 'package:acanthis/acanthis.dart';
void main() {
final jwt = string().jwt();
final result = jwt.tryParse('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
base64
The base64 method is used to validate a base64 string.
import 'package:acanthis/acanthis.dart';
void main() {
final base64 = string().base64();
final result = base64.tryParse('SGVsbG8gV29ybGQh');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
letters
The letters method is used to validate a string that contains only letters.
import 'package:acanthis/acanthis.dart';
void main() {
final letters = string().letters();
final result = letters.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
It can be used with the strict
parameter to allow spaces and special characters.
import 'package:acanthis/acanthis.dart';
void main() {
final letters = string().letters(strict: false);
final result = letters.tryParse('Acanthis 1.0');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
digits
The digits method is used to validate a string that contains only digits.
import 'package:acanthis/acanthis.dart';
void main() {
final digits = string().digits();
final result = digits.tryParse('123456');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
It can be used with the strict
parameter to allow spaces and special characters.
import 'package:acanthis/acanthis.dart';
void main() {
final digits = string().digits(strict: false);
final result = digits.tryParse('123 456');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
alphanumeric
The alphanumeric method is used to validate a string that contains only alphanumeric characters.
import 'package:acanthis/acanthis.dart';
void main() {
final alphanumeric = string().alphanumeric();
final result = alphanumeric.tryParse('Acanthis10');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
It can be used with the strict
parameter to allow spaces and special characters.
import 'package:acanthis/acanthis.dart';
void main() {
final alphanumeric = string().alphanumeric(strict: false);
final result = alphanumeric.tryParse('Acanthis 1.0');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
alphanumericWithSpaces
The alphanumericWithSpaces method is used to validate a string that contains only alphanumeric characters and spaces.
import 'package:acanthis/acanthis.dart';
void main() {
final alphanumericWithSpaces = string().alphanumericWithSpaces();
final result = alphanumericWithSpaces.tryParse('Acanthis 10');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
It can be used with the strict
parameter to allow special characters.
import 'package:acanthis/acanthis.dart';
void main() {
final alphanumericWithSpaces = string().alphanumericWithSpaces(strict: false);
final result = alphanumericWithSpaces.tryParse('Acanthis 1.0!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
specialCharacters
The specialCharacters method is used to validate a string that contains only special characters.
import 'package:acanthis/acanthis.dart';
void main() {
final specialCharacters = string().specialCharacters();
final result = specialCharacters.tryParse('!@#\$%^&*()');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
It can be used with the strict
parameter to allow spaces and alphanumeric characters.
import 'package:acanthis/acanthis.dart';
void main() {
final specialCharacters = string().specialCharacters(strict: false);
final result = specialCharacters.tryParse('!@#\$%^&*() Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
allCharacters
The allCharacters method is used to validate a string that contains all characters.
import 'package:acanthis/acanthis.dart';
void main() {
final allCharacters = string().allCharacters();
final result = allCharacters.tryParse('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#\$%^&*()');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
upperCase
The upperCase method is used to validate a string that is in uppercase.
import 'package:acanthis/acanthis.dart';
void main() {
final uppercase = string().upperCase();
final result = uppercase.tryParse('HELLO WORLD!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
lowerCase
The lowerCase method is used to validate a string that is in lowercase.
import 'package:acanthis/acanthis.dart';
void main() {
final lowercase = string().lowerCase();
final result = lowercase.tryParse('hello world!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
mixedCase
The mixedCase method is used to validate a string that is in mixed case.
import 'package:acanthis/acanthis.dart';
void main() {
final mixedCase = string().mixedCase();
final result = mixedCase.tryParse('Hello World!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
dateTime
The dateTime method is used to validate a string that is a valid date time.
import 'package:acanthis/acanthis.dart';
void main() {
final dateTime = string().dateTime();
final result = dateTime.tryParse('2021-10-10');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
uri
The uri method is used to validate a string that is a valid uri.
import 'package:acanthis/acanthis.dart';
void main() {
final uri = string().uri();
final result = uri.tryParse('https://acanthis.dev');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
uncompromised
The uncompromised method is used to validate a string that is not compromised.
WARNING
This method uses the haveibeenpwned
API to check if the password is compromised. This check is an async check.
import 'package:acanthis/acanthis.dart';
void main() {
final uncompromised = string().uncompromised();
final result = uncompromised.tryParseAsync('password');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
or
The or
method is used to create a AcanthisUnion
from the current schema and the provided schemas.
import 'package:acanthis/acanthis.dart';
void main() {
final schema = string().or([string().min(3), string().max(5)]);
final result = schema.tryParse('Acanthis');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
list
The list method is used to validate a list of String
objects.
For the list validations methods, you check the List page.
import 'package:acanthis/acanthis.dart';
void main() {
final list = string().min(3).list();
final result = list.tryParse(['Acanthis', '255']);
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
}
encode
The encode method is used to encode a string to base64.
import 'package:acanthis/acanthis.dart';
void main() {
final base64 = string().encode();
final result = base64.tryParse('Hello World!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
/// result.value == 'SGVsbG8gV29ybGQh'
}
decode
The decode method is used to decode a base64 string.
import 'package:acanthis/acanthis.dart';
void main() {
final base64 = string().decode();
final result = base64.tryParse('SGVsbG8gV29ybGQh');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
/// result.value == 'Hello World!'
}
toUpperCase
The toUpperCase method is used to convert a string to uppercase.
import 'package:acanthis/acanthis.dart';
void main() {
final uppercase = string().toUpperCase();
final result = uppercase.tryParse('hello world!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
/// result.value == 'HELLO WORLD!'
}
toLowerCase
The toLowerCase method is used to convert a string to lowercase.
import 'package:acanthis/acanthis.dart';
void main() {
final lowercase = string().toLowerCase();
final result = lowercase.tryParse('HELLO WORLD!');
if (result.success) {
print('The schema is valid!');
} else {
print('The schema is invalid!');
}
/// result.value == 'hello world!'
}