Code Monkey home page Code Monkey logo

Comments (3)

rhalff avatar rhalff commented on September 15, 2024

Hi, are you referring to https://github.com/typestack/class-validator written in typescript?

The description for @IsOptional() over there is:

Checks if given value is empty (=== null, === undefined) and if so, ignores all the validators on the property.

This seems to be different from what you're asking though (optional attributes) or do you mean the same thing?

This library currently doesn't have any conditional validators, I could have a look this week though as it seems doable.

from validations.

 avatar commented on September 15, 2024

Hello, this is exactly what I meant. I would like to provide validations for optional properties, which would be enforced only when the property is actually set.
Thank you for the feedback!

from validations.

timothychristensen avatar timothychristensen commented on September 15, 2024

@Andreas-Hubnao - we simply leverage the existing classes and create our own with the idea of NullOr...

Although I agree the @IsOptional() would be fantastic.

For example:

@immutable
@Constraint(validatedBy: NullOrSizeValidator)
@Target({ElementType.FIELD})
class NullOrSize extends ValidatorAnnotation {
  final int min;
  final int max;
  const NullOrSize({
    this.min,
    this.max,
    String message,
    List<String> groups,
  }) : super(message, groups);
}
class NullOrSizeValidator extends ConstraintValidator {
  final int max;
  final int min;

  NullOrSizeValidator({
    @required this.min,
    @required this.max,
  })  : assert(min != null),
        assert(max != null),
        super([min, max]);

  @override
  bool isValid(dynamic value, ValueContext context) {
    if (value == null) {
      return true;
    }

    if (value is String || value is Iterable || value is Map) {
      return min <= (value.length as int) && (value.length as int) <= max;
    }

    if (value is num) {
      return min <= value && value <= max;
    }

    return false;
  }

  @override
  Function message = (int min, int max, Object validatedValue) => '$validatedValue is not between $min and $max';
}

You'll only need to make sure you import BOTH the validator and the annotation, for example:

import 'package:yourpackage/src/validation/null_or_size.dart';
import 'package:yourpackage/src/validation/null_or_size_validator.dart';

We find this to be common for APIs requiring specific sizes of string values - or - they can be null.

from validations.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.