Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default "max" or "min" values? #647

Open
Will-at-FreedomDev opened this issue Mar 13, 2024 · 1 comment
Open

Default "max" or "min" values? #647

Will-at-FreedomDev opened this issue Mar 13, 2024 · 1 comment

Comments

@Will-at-FreedomDev
Copy link

Describe the problem statement?

Is it possible to set the max/min values for decorators from a global config level?

I ask because there are certain min/max values that must be followed depending on your back-end. For example, the minimum date in SQL Server is 1753-1-1. Instead of adding a min date validator with this value specifically, it would be nice if I could just add the minDate validator and have it default to this value. This could be extended to numbers as well if you wanted to have the default min-max to be within the integer range. Not sure what else we could apply this to (maybe validation messages).

Example:

export class MyDateClass {
  @minDate({ value: new Date(1753, 0, 1) })
  myDateProp: Date;
}

The expected solution you would like

I'd like to be able to set a value from a global place. Preferably from an injector like

providers: [
  {
    provide: RXWEB_VALIDATOR_MIN_DATE_DEFAULT_OPTIONS,
    useValue: { value: new Date(1753, 0, 1) }
  }
]

My class could then be

export class MyDateClass {
  @minDate()
  myDateProp: Date;
}

Package version

"@rxweb/reactive-form-validators": "^13.0.1",

@Will-at-FreedomDev
Copy link
Author

Will-at-FreedomDev commented Apr 5, 2024

This can probably be closed. I wrote my own property decorator to implement this.

import { compose, RxwebValidators } from "@rxweb/reactive-form-validators";

export const number = (config?: { min?: number; max?: number }) => {
  const min = config?.min ?? -9999999999999999.99;
  const max = config?.max ?? 9999999999999999.99;

  const validators = [
    RxwebValidators.minNumber({ value: min }),
    RxwebValidators.minNumber({ value: max }),
  ];

  const validate = compose({ validators });

  return (target: any, propertyKey: string) => {
    validate(target, propertyKey);
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant