Skip to main content

Types

By default, ZodArt parsers operate in strict mode, see more about parsers here.

Strings

Corresponds to the Dart String type.

Definition:

ZString();
ZString().nullable();
ZString().optional();

Validations:

ZString().min(1);
ZString().max(7);
ZString().regex('^[a-z]*$');

Value processing:

ZString().toLowerCase();
ZString().toUpperCase();
ZString().trim();

Type transformations:

ZString().toInt();
ZString().toDouble();
ZString().toDateTime();

Integers

Corresponds to the Dart int type.

Definition:

ZInt();
ZInt().nullable();
ZInt().optional();

Validations:

ZInt().min(1);
ZInt().max(7);

Type transformations:

ZInt().toStr((val) => '$val');
ZInt().toDouble();

Decimals

Corresponds to the Dart double type.

Definition:

ZDouble();
ZDouble().nullable();
ZDouble().optional();

Validations:

ZDouble().min(1.0);
ZDouble().max(7.0);

Type transformations:

ZDouble().toStr((val) => '$val');
ZDouble().toInt((val) => val.round());

Date and time

Corresponds to the Dart DateTime type.

Definition:

ZDateTime();
ZDateTime().nullable();
ZDateTime().optional();

Validations:

ZDateTime().min(DateTime.now());
ZDateTime().max(DateTime.now());

Type transformations:

ZDateTime().toStr((val) => val.toString());

Boolean

Corresponds to the Dart bool type.

Definition:

ZBool();
ZBool().nullable();
ZBool().optional();

Type transformations:

ZBool().toStr((val) => val.toString());

Iterables

Corresponds to the Dart List<T> type.

Definition:

ZArray(ZString());
ZArray(ZString()).nullable();
ZArray(ZString()).optional();

Validations:

ZArray(ZString()).min(1);
ZArray(ZString()).max(7);

Type transformations:

ZArray(ZString()).toStr((vals) => vals.join(','));
ZArray(ZInt()).toArray(myIntsToStrings); // Change the type from List<int> to List<String>

Complex objects

Corresponds to the Dart records or instances based on a class.

ZObject definition

Code generation

New class based on your schema

ZodArt automatically generates the class representation code including the .toString(), .fromJson() methods and the equality code.

.generateNewClass(outputClassName: 'Item')
abstract class ItemSchema {
// The schema used to generate the class
static final schema = (
id: ZInt(),
name: ZString().optional(),
);

static const z = _ItemSchemaUtils();
static final ZObject<Item> zObject = z.zObject;
}

// ZObject<Item>
ItemSchema.zObject;
Existing class

To reuse your existing models (e.g. freezed classes), ZodArt automatically selects the best constructor and generates all the necessary boilerplate code to instantiate the class.

class Item {
/// class code including a valid public constructor
}

.withExistingClass(outputClassType: Item)
abstract class ItemSchema {
static final schema = (
id: ZInt(),
name: ZString().nullable(),
);

static const z = _ItemSchemaUtils();
static final ZObject<Item> zObject = z.zObject;
}

// ZObject<Item>
ItemSchema.zObject;
Dart Record

To a Dart record as output, ZodArt automatically generates all the necessary boilerplate code to create the record.

typedef Item = ({int id, String name});

.withRecord(outputRecordType: Item)
abstract class ItemSchema {
static final schema = (
id: ZInt(),
name: ZString(),
);

static const z = _ItemSchemaUtils();
static final ZObject<Item> zObject = z.zObject;
}

// ZObject<Item>
ItemSchema.zObject;

No code generation

⚠️ Using code generation is HIGHLY recommended

Without code generation you are required to write the .fromJson() method by yourself. Even reusing .fromJson() from tools like freezed can be error-prone due to potential mismatches between the schema and the actual output.

// ZObject<({int id, String name})>
ZObject.withMapper(
{
'id': ZInt(),
'name': ZString(),
},
// Mapper required to create the output
fromJson: (Map<String, dynamic> rawData) => (
id: rawData['id'] as int,
name: rawData['name'] as String
),
);

Other methods

Nullability:

myZObj.nullable();
myZObj.optional();

Type transformations:

myZObj.toStr(myObjToString);
myZObj.toObj(myObjToMyNewObj); // Change the type from MyObj to MyNewObj