0.1.3 • Published 1 year ago

@stageus/validator v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

🚀 @stageus/validator

You can use @stageus/validator to validate different types of data.

@stagues/valdiator allows you to transform or validate not only string type but also various types of data.

Installation

npm install @stageus/validator

✏️ Usage

This library provides validation for Type as well as Boolean, Date, Number, String, Array, NumberArray, and StringArray types.

Firstly, a function called 'message' provides Type validation. Here, you can validate the type and use the basic utility method for validation. Once the type of data to be validated is confirmed, you can define and use the method to validate the type through each type validation method in a chaining manner.

import { message } from '@stageus/validator';

const emailValdiator = message().isString().isEmail();

const result = emailValdiator.run('abc123@xx.xx');
result.valid; // true
result.message; // null
result.value; // abc123@xx.xx

const result2 = emailValidator.run('abc123');
result2.valid; // false
result2.message; // Value is not email format
result2.value; // abc123

Also you can use like below.

const signUpSchema = object({
  email: message('Invalid Email').isString().isEmail()
  pw: message('Invalid Pw').isString().isPw(),
  fileList: array({
    path: message('Invalid file path').isString().length({ min: 1 }),
    ext: message('Invalid file ext').isString().isIn(['png', 'jpeg'])
  })
});
const result3 = signUpSchema.run(userInputData);

Don't forget that you have to call run method in order to execute the chained method.

📄 Document

🔧 Utility

You can use the TypeValidator method through the function named message. The Type validation method can be confirmed through the document below.

message()

You can set an exception message. If it's empty, the default message is output.

message(); // then you can use Type method

message('invalid email').isString().isEmail();

Never forget to call run method.

🔧 Schema

ObjectSchema and ArraySchema can be created through the object function and array function, and the validation schema can be called through the run method to evaluate the value.

object()

It is a funciton of creating an ObjectSchema.

const signUpSchema = object({
  email: message('invalid email').toString().isEmail(),
  pw: message('invalid pw').toString().legnth({ min: 6, max: 12 }).isPw(),
});

const result = signUpSchema.run({ email: 'abc123@xx.xx', pw: '123' });
result.valid; // false
result.value; // { email: "abc123@xx.xx", pw: "123"}
result.reason; // [{ message: "invalid pw", field: "pw" }]

object function returns ObjectSchema.

run(data: any)

Execute validate schema with data.

object({}).run(); // SchemaRunResult

Never forget to call run method.

signUpSchema.run(userInput);

array(any)

It is a funciton of creating an ArraySchema.

Through array function, the value of the object array is validated.

array(signUpSchema);

This function does not support the run method.

To validate, the object function must be used.

array(signUpSchema).run(input);

You can also mix and use functions like below.

const signUpSchema = object({
  email: message('invalid email').isString().isEmail(),
  pw: message('invalid pw').isString().length({ min: 6, max: 12 }),
  info: {
    imgList: array({
      path: message('invalid path').isString().length({ min: 1 }),
      ext: message('invalid ext').isString().isIn(['png', 'jpg']),
    }),
  },
});

Then you can call run method to limit legnth of the array.

signUpSchema.run(data);

length(option)

Also you can use length mehtod.

array(signUpSchema).length({ min: 3 });

🔧 Type Method

Method NameReturnDescription
optional()TypeIn case of null or undefined, the chaining method is ignored and the validation result is set to true.
default(value)TypeIf the value is null or undefined, it is validated with the value put as the first parameter.
isNumber(option)NumberIt checks if the input value is of type number. By default, numbers entered as a string are also allowed, but if the strict option is set to true, all strings fail the validation.
isArray()ArrayIt checks if it is an array type.
isString(option)StringIt checks if it is a string type. By default, only strings are allowed, but if allowNumber is set to true, it also converts numbers to strings and passes the validation.
isBoolean()BooleanIt checks if it is a boolean type. By default, only true or false pass the validation, but if the strict option is set to true, "true", "false", 0, and 1 also pass the validation.
isDate()DateIt checks whether the input value can be converted to a Date object. If the input value is a string, it should basically follow the ISO8061 format.
isEmpty()It checks if it is empty. Since this method only checks whether it is empty, you cannot use more validation method chaining.

🔧 Number Method

Method NameReturnDescription
range(option)NumberIt checks whether the input value falls within the range received as a parameter. The first parameter must have a min or max property. If not entered, it is basically evaluated as Infinite or -Infinite.
isIn(numberList)NumberIt checks whether the input value exists within the first parameter array.
isNotIn(numberList)NumberIt checks whether the input value does not exist within the first parameter array.
isInt()NumberIt checks if it is an Integer.
isPort()NumberIt checks if it can be used as a port number.
toInt()NumberIt converts to an integer. In the process, decimal places are discarded.
toString()StringThe value is changed to a string type, and now you can use the String method.
toBoolean()BooleanThe value is changed to a Boolean type, and now you can use the Boolean method.

🔧 String Method

Method NameReturnDescription
isEmail()StringValidates whether the input follows the standard email format.
isJson()StringChecks if the input can be parsed into a valid JSON object.
isHanguel(option)StringThis is a method to check if the string is composed of Korean characters. The complete option is false by default, and if set to true, only completed Korean characters are allowed. The space option is false by default, and if set to true, spaces are allowed.
length(option)StringThis is a method to check the length of a string. The first parameter option must include the min or max property, and by default, it checks for -Infinity or Infinity.
match(regExp)StringThis is a method to check if the input value satisfies the regular expression.
isOnlyAlphabet()StringThis is a method to check if the string is composed only of alphabets.
isDate()StringThis is a method to check if the format is a date. e.g. YYYY-MM-DD
isDateTime()StringThis is a method to check if the format is a date with time. e.g. YYYY-MM-DDTHH:MI:SS
isPw(option)StringThis is a method to check if the format is a password. The number option is true by default, and if set to false, you can prevent the string from including numbers. The uppercase option is false by default, and if set to true, uppercase letters must be included. Strong is false by default, and if set to true, special characters must be included.
isJwt()StringThis is a method to check if the string is in JWT format.
isEndWith(endStr)StringThis is a method to check if the string ends with the string received as the first parameter.
isStartWith(startStr)StringThis is a method to check if the string starts with the string received as the first parameter.
include(includeStr)StringThis is a method to check if the string includes the string received as the first parameter.
toNumber()NumberThis is a method that changes it to a number type. Now you can use the Number method. If the string cannot be changed to a number, the validation fails and the value does not change.
toInt()NumberThis is a method that converts the string to an integer. Now you can use the Number method. If the string cannot be changed to a integer, the validation fails and the value does not change.
toDate()DateThis is a method that changes it to a Date type. Now you can use the Date method. If the string cannot be changed to a date, the validation fails and the value does not change.
toBoolean()BooleanThis is a method that changes it to a Boolean type. Now you can use the Boolean method. If the string cannot be changed to a Boolean, the validation fails and the value does not change.
split()String ArrayThis is a method that splits the string by the character received as the first parameter. Now you can use the String Array method.
trim()StringThis is a method that removes the whitespace from both ends of the string.
rTrim()StringThis is a method that removes the whitespace from the left end of the string.
lTrim()StringThis is a method that removes the whitespace from the right end of the string.

🔧 Date Method

Method NameReturnDescription
isBefore(date)DateThis is a method that checks if the date is earlier than the date received as the first parameter.
isAfter(date)DateThis is a method that checks if the date is later than the date received as the first parameter.

🔧 Array Method

Method NameReturnDescription
length(option)ArrayThis is a method to check the length of an array. The first parameter option must include the min or max property, and by default, it is evaluated as Infinity or -Infinity.
isAllNotNull()ArrayThis is a method to check if all elements of the array are not Null or Undefined.
isALlIf(func)ArrayThis is a method that evaluates all values in the array with a custom function that returns a boolean type.
isAllString()String ArrayThis is a method that checks if all values in the array are of String Type. Now you can use the String Array method.
isAllNumber()Number ArrayThis is a method that checks if all values in the array are of Number Type. Now you can use the Number Array method.

🔧 Number Array Method

Method NameReturnDescription
isAllInt()Number ArrayThis is a method that checks if all elements of the array are of integer type.
isAllIn(numberList)Number ArrayThis is a method that checks if all elements of the array are one of the values in the number array received as the first parameter.
isAllNotIn(numberList)Number ArrayThis is a method that checks if all elements of the array are not included in the number array received as the first parameter.
isAllPort()Number ArrayThis is a method that checks if all elements of the array can be used as port numbers.
allRange(option)Number ArrayThis is a method that checks if all elements of the array are within the range. The first parameter must have a min or max property, and by default, it is evaluated as Infinity or -Infinity.

🔧 String Array Method

Method NameReturnDescription
isAllMatch(regExp)String ArrayThis is a method that checks if all elements of the array satisfy the regular expression.
isAllJwt()String ArrayThis is a method that checks if all elements of the array are in the Jwt format.
isAllJson()String ArrayThis is a method that checks if all elements of the array are JSON.
allInclude(includeStr)String ArrayThis is a method that checks if all elements of the array contain the string received as the first parameter.
allLength(option)String ArrayThis is a method that checks if the length of all strings in the array falls within the range. The first parameter must have a min or max property, and by default, it is evaluated as Infinity or -Infinity.
isAllStartWith(startStr)String ArrayThis is a method that checks if all strings in the array start with the string received as the first parameter.
isAllEndWith(endStr)String ArrayThis is a method that checks if all elements of the array end with the string received as the first parameter.
isAllHanguel(option)String ArrayThis is a method that checks if all elements of the array are in Korean. By default, complete is false and only complete Korean characters pass the validation when it's true. The space option is false by default, and if it's true, the validation succeeds even if there is a space.
isAllOnlyAlphabet()String ArrayThis is a method that checks if all elements of the array are strings made up of only alphabets.
isAllEmail()String ArrayThis is a method that checks if all elements of the array are in email format.
isAllDateTime()String ArrayThis is a method that checks if all elements of the array are in a date format that includes time.

🔧 Boolean Method

There are no methods yet.

Maintainer

LICENSE

LICENSE

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago