ion_extra 0.2.0 copy "ion_extra: ^0.2.0" to clipboard
ion_extra: ^0.2.0 copied to clipboard

JSON parsing, query decoding, validation, and schema generation for ion_web.

example/example.dart

import 'package:ion_extra/ion_extra.dart';
import 'package:ion_web/ion_web.dart';

class LoginDto {
  LoginDto.fromJson(JsonObject json)
    : email = json.string('email', rules: [.email()]),
      password = json.string('password');

  static final Schema schema = JsonObject.schema(
    LoginDto.fromJson,
    title: 'LoginDto',
  );

  final String email;
  final String password;
}

class AccessTokenDto implements ToJson {
  AccessTokenDto({required this.accessToken});

  final String accessToken;

  @override
  Map<String, Object?> toJson() => {'access_token': accessToken};
}

Future<void> main() async {
  print('--- LoginDto Schema ---');
  print(LoginDto.schema);

  final server = await IonServer.serve(
    (req) async {
      try {
        final payload = await req.json(LoginDto.fromJson);
        if (payload.email != 'ion@example.com' || payload.password != 'ion') {
          return .text('invalid credentials', status: .unauthorized);
        }

        return Json(AccessTokenDto(accessToken: '90f1ae0b7ace00a7659775a0'));
      } on ValidationErrors catch (error) {
        return Json(error, status: .badRequest);
      }
    },
    address: .loopbackIPv4,
    port: 3000,
  );

  print('Server listening on http://${server.address.host}:${server.port}');
}
0
likes
160
points
191
downloads

Documentation

API reference

Publisher

verified publisherpusk.software

Weekly Downloads

JSON parsing, query decoding, validation, and schema generation for ion_web.

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

equatable, ion_web, json_codable, meta

More

Packages that depend on ion_extra