lemonade 0.1.0 copy "lemonade: ^0.1.0" to clipboard
lemonade: ^0.1.0 copied to clipboard

outdated

Simple, typesafe, object-oriented library for data validation

lemonade #

Simple yet powerful library for data validation:

  • Fully typesafe and null-safe API
  • Universal for any standard data structures
  • Written in pure dart
  • 0 dependencies

Get started 🚀 #

First, add lemonade into pubspec.yaml file of your project:

dependencies:
  lemonade: ^0.1.0

... or with simple command:

dart pub add lemonade
# or for flutter project:
flutter pub add lemonade

Then import it into your dart file:

import 'package:lemonade/lemonade.dart';

And you are ready to go!

Usage #

For example, you want to validate GeoJSON point object that looks like this:

{
    "type": "Point",
    "coordinates": [125.6, 10.1]
}

Validator for this schema will look like that:

final pointsValidator = Validator.object(
  items: {
    // "type": "Point"
    'type': Validator.equals('Point'),
    // "coordinates": [125.6, 10.1]
    'coordinates': Validator.list(
      item: Validator.number(),
      // Length exactly 2
      minItems: 2,
      maxItems: 2,
    ),
  },
);

Then you can check if any data looks like with very basic method:

final decodedData = jsonDecode('{"type":"Point","coordinates":[125.6,10.1]}');

print(pointsValidator.validate(decodedData)); // true

If you want to check, what exactly is wrong, you can use getError method:

final wrongData = jsonDecode('{"type":"Point","coordinates":[125.6,"10.1"]}');

print(pointsValidator.getError(wrongData)); // object.coordinates > list<number>[1] > expected(number).got(10.1)

String representation of the errors says that:

  • In field "coordinates"
    • At index [1]
      • Expected any number
      • Got "10.1"

It's easy to decode and trace an error in your data, isn't it?

4
likes
0
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

Simple, typesafe, object-oriented library for data validation

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on lemonade