@react-input-validator/rules
Version:
The validation rule objects used by the packages: `@react-input-validator/core`, `@react-input-validator/native` and `@react-input-validator/web`
45 lines (39 loc) • 1.61 kB
text/typescript
/**
* https://github.com/atmulyana/react-input-validator
*/
import type {IRule, Nullable} from './Rule';
import ValidationRule from "./ValidationRule";
import messages from './messages';
export class BooleanValue extends ValidationRule<any, boolean | null> {
#resultValue: Nullable<boolean>;
get errorMessage() {
return this.lang(messages.boolean);
}
get resultValue(): boolean | null {
return this.#resultValue === undefined ? this.valueAsBoolean : this.#resultValue;
}
get valueAsBoolean(): boolean | null {
if (this.value === true || this.value === false) return this.value;
if (typeof(this.value) == 'string') {
const strVal = this.value.toLowerCase();
if (strVal == 'true') return true;
if (strVal == 'false') return false;
}
return null;
}
validate() {
this.#resultValue = this.valueAsBoolean;
this.isValid = this.#resultValue !== null;
return this;
}
}
export const boolean: IRule<any, boolean | null> = new BooleanValue();
boolean.arrayAsSingle = function() {
throw new Error("`boolean` rule object is shared among inputs. If you want to invoke `arrayAsSingle`, use `new BooleanValue()` instead.");
};
boolean.setMessageFunc = function() {
throw new Error("`boolean` rule object is shared among inputs. If you want to set message, use `new BooleanValue()` instead.");
};
boolean.setPriority = function() {
throw new Error("`boolean` rule object is shared among inputs. If you want to set priority, use `new BooleanValue()` instead.");
};