celest-dev / celest

The Flutter cloud platform
https://celest.dev
Other
232 stars 12 forks source link

Enhancing Global Data Validation Capabilities in Celest #41

Open X-SLAYER opened 4 months ago

X-SLAYER commented 4 months ago

Overview

Backend development requires robust data validation to protect against invalid or harmful data. With increasing application complexity, advanced, user-friendly methods are needed. Celest's strong backend foundation could be enhanced with a specialized package like ez_validator

Proposed Solution: ez_validator

ez_validator stands out as an exemplary solution for several reasons:

Implementation Use Cases

Benefits to Celest

How EzValidator can help

ez_validator offers a dead-simple approach to field and object schema validation tailored for Flutter. Inspired by the intuitive API of Yup, ez_validator simplifies the process of defining and enforcing data schemas within your Flutter applications.

final bodySchema = EzSchema.shape({
  'name': EzValidator<String>()
      .transform((value) => value.trim())
      .minLength(3, 'Name must be at least 3 characters long.'),
  'password': EzValidator<String>()
      .minLength(8, 'Password must be at least 8 characters long.'),
  'confirmPassword': EzValidator<String>().when(
    'password',
    (confirmValue, [ref]) =>
        confirmValue == ref?['password'] ? null : 'Passwords do not match',
  )
})

/// Validate the coming data 
final errors = bodySchema.catchErrors(bodyDaya); // Example bodyDaya is a Map<String,String>

ez_validator handle complex validations

  final EzSchema userProfileSchema = EzSchema.shape({
    "firstName": EzValidator<String>().required(),
    "lastName": EzValidator<String>().required(),
    "email": EzValidator<String>().required().email(),
    "age": EzValidator<int>().min(18).max(100),
    'contactDetails': EzSchema.shape({
      'mobile': EzValidator<String>()
          .required()
          .matches(RegExp(r'^\+\d{10,15}$'), 'Invalid phone number'),
      'landline': EzValidator<String?>(optional: true),
    }),
    'level': EzValidator<String>(defaultValue: 'Beginner').required().oneOf([
      'Beginner',
      'Intermediate',
      'Advanced',
      'Expert',
    ]),
    'address': EzSchema.shape({
      'street': EzValidator<String>().required(),
      'city': EzValidator<String>().required(),
      'state': EzValidator<String>().required(),
      'zipCode': EzValidator<num>().required(),
      'country': EzSchema.shape({
        'name': EzValidator<String>(defaultValue: 'TUNISIA').required(),
        'code': EzValidator<String>().required(),
        'continent': EzSchema.shape({
          'name': EzValidator<String>().required(),
          'code': EzValidator<String>().required(),
        })
      }),
    }),
    'employment': EzSchema.shape({
      'current': EzValidator<String?>(optional: true),
      'previous': EzSchema.shape({
        'companyName': EzValidator<String>().required(),
        'position': EzValidator<String>().required(),
        'years': EzValidator<int>().min(1).max(50),
      }),
    }),
  });

I look forward to further discussions on this and am eager to contribute to the implementation of this feature.