cyrtophora

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 0141f17fa88eb08d111d4188da57ec801fc8be0e
parent 3ac5e1c4a8f5bab61bc9b23bdc4b55e588360ab2
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Wed, 31 Aug 2022 16:52:07 +0200

[cyrto] add data.dart for handling API data

Diffstat:
Mcyrto/lib/cyrtophora.dart | 1+
Acyrto/lib/src/data.dart | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/cyrto/lib/cyrtophora.dart b/cyrto/lib/cyrtophora.dart @@ -4,3 +4,4 @@ library cyrtophora; export 'src/utils.dart'; export 'src/validate.dart'; +export 'src/data.dart'; diff --git a/cyrto/lib/src/data.dart b/cyrto/lib/src/data.dart @@ -0,0 +1,64 @@ +import "package:cyrtophora/cyrtophora.dart"; + +/// Account registration input +class AccountRegistration { + String username; + String email; + String password; + String retyped_password; + + AccountRegistration( + this.username, this.email, this.password, this.retyped_password); + + /// Generate input from JSON + factory AccountRegistration.fromJson(dynamic json) { + return AccountRegistration( + json['username'] as String, + json['email'] as String, + json['password'] as String, + json['retyped_password'] as String); + } + + /// Serialize object to JSON + Map<String, String> toJson() { + return { + 'username': username, + 'email': email, + 'password': password, + 'retyped_password': retyped_password + }; + } + + /// Validate input and create + bool isValid() { + // Check if password and retyped password match + if (password != retyped_password) { + throw Exception('InvalidPasswordException\n\n- passwords do not match'); + } + // Validate username + if (!Validate.username(username)) { + throw Exception( + 'InvalidUsernameException\n\n- Username cannot be empty\n- Username cannot be more than 15 characters long\n- Username cannot contain symbols expcept one _'); + } + + // Validate fullname + // if (!Validate.fullname(fullname)) { + // //throw InvalidNameException; + // throw Exception( + // 'InvalidNameException\n\n- Name cannot be empty\n- Name cannot contain more than 70 characters'); + // } + + // Validate password + if (!Validate.password(password)) { + throw Exception( + 'InvalidPasswordException\n\n- Password length should be 10 chartcers or more'); + } + + // Validate email + if (!Validate.email(email)) { + throw Exception('InvalidEmailException\n\n- Invalid email'); + } + + return true; + } +}