warning-message
A declarative way to return warning messages
Why?
Almost any project needs a warning messages.
This package simplifies your workflow.
Installation
npm i -S warning-message
or
yarn add warning-message
Usage
After installation you can import the package to your project and use a declarative way return warning messages.
The return value is always a string
.
You can use the default warning messages or create your own custom warning messages.
Simple example with default warning messages
If a function takes no parameter it returns the standard message.
If a function takes an parameter, it simple returns this string.
import warningMessage from 'warning-message';
warningMessage().email();
warningMessage().email('Not an email!')
Advanced example with default warning messages
Some functions have 2 or more parameters. The last parameter is always a function that takes the previous parameters. You can use this function to create a custom string.
import warningMessage from 'warning-message';
warningMessage().minMaxChar(3, 6);
warningMessage().minMaxChar(3, 6, (min, max) => `input should have min: ${min} and max: ${max} letters`);
Advanced example with your own custom warnings
Create file where you export your custom warnings:
Note: the properties: exactly
, maxChar
, minChar
, and not
can have a __VAR1__
Not: the property: minMaxChar
can have a __VAR1__
and a __VAR2__
__VAR1__
& __VAR2__
are the additional values passed to the function. you can add them if you want but you dont have to.
const customWarnings = {
date: 'Custom warning: date format!',
datetime: 'Custom warning: datetime format!',
email: 'Custom warning: email!',
exactly: 'Custom warning: exactly __VAR1__!',
float: 'Custom warning: float!',
integer: 'Custom warning: integer!',
maxChar: 'Custom warning: max. __VAR1__ letters!',
minChar: 'Custom warning: min. __VAR1__ letters!',
minMaxChar: 'Custom warning: min. __VAR1__ and max. __VAR2__ letters!',
not: 'Custom warning: not __VALUE__!',
number: 'Custom warning: number!',
phonenumber: 'Custom warning: phonenumber!',
requiredField: 'Custom warning: required!',
time: 'Custom warning: time format!',
url: 'Custom warning: url!',
validation: 'Custom warning: not valid!',
};
export default customWarnings;
In you file where you would use the warnings you can use your custom warnings like that:
import warningMessage from 'warning-message';
import customWarnings from 'path/to/your/customWarnings';
warningMessage(customWarnings).minMaxChar(3, 5);
warningMessage(customWarnings).phonenumber();
warningMessage(customWarnings).not('this');
Functions:
date
This function returns a warning for a date.
warningMessage().date();
warningMessage().date('custom message');
datetime
This function returns a warning for a datetime.
warningMessage().datetime();
warningMessage().datetime('custom message');
email
This function returns a warning for an email.
warningMessage().email();
warningMessage().email('custom message');
exactly
This function returns a warning for an value that do not match exactly a target.
warningMessage().exactly('target');
warningMessage().exactly('target', (target) => `custom message ${target}`);
float
This function returns a warning for a float.
warningMessage().float();
warningMessage().float('custom message');
integer
This function returns a warning for an integer.
warningMessage().integer();
warningMessage().integer('custom message');
maxChar
This function returns a warning for a value that exceeds a target.
warningMessage().macChar(6);
warningMessage().maxChar(6, (max) => `custom message ${mex}`);
minChar
This function returns a warning for a value that has less character than a target.
warningMessage().minChar(3);
warningMessage().minChar(3, (min) => `custom message ${min}`);
minMaxChar
This function returns a warning for a value that has less character than a target and eceeds a second target.
warningMessage().minMaxChar(3, 6);
warningMessage().minMaxChar(3, 6, (min, max) => `custom message ${min} and ${max}`);
not
This function returns a warning for a value that is not alowed.
warningMessage().not('value');
warningMessage().not('value', (value) => `custom message ${value}`);
number
This function returns a warning for a number.
warningMessage().number();
warningMessage().number('custom message');
phonenumber
This function returns a warning for a phonenumber.
warningMessage().phonenumber();
warningMessage().phonenumber('custom message');
requiredField
This function returns a warning for a required inputfield.
warningMessage().requiredField();
warningMessage().requiredField('custom message');
time
This function returns a warning for a time.
warningMessage().time();
warningMessage().time('custom time warning message');
url
This function returns a warning for a url.
warningMessage().url();
warningMessage().url('custom message');
validation
This function returns a warning for a validation.
warningMessage().validation();
warningMessage().validation('custom message');
LICENSE
MIT © Lukas Aichbauer