4.0.2 • Published 2 years ago

@bakku/validation v4.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

1 Install & Use

1.1 Install

npm install @bakku/validation

1.2 Use

Recommended use asyncValidate function for all validation.

Return an error if it is failed the validation, return correct value of data if it passed the validation

Refer: use asyncValidate

asyncValidate = async <T = any>(
  schema?: ISchemaCore<T>,
  data?: T,
  name?: string
): Promise<IValidationError | TypeOptional<T>> => {

1.2.1 asyncValidateString

Return an error if it is failed the validation, return string (as inputted data) if it passed the validation Refer: use asyncValidateString

asyncValidateString = async (
  schema: IStringSchema,
  data?: any,
  name?: string
): Promise<IValidationError | string>

1.2.2 asyncValidateNumber

Return an error if it is failed the validation, return number (as inputted data or converted data) if it passed the validation Refer: use asyncValidateNumber

asyncValidateNumber = async (
  schema: INumberSchema,
  data?: any,
  name?: string
): Promise<IValidationError | number> => {

1.2.3 asyncValidateInteger

Return an error if it is failed the validation, return integer (as inputted data or converted data) if it passed the validation Refer: use asyncValidateInteger

asyncValidateInteger = async (
  schema: IIntegerSchema,
  data?: any,
  name?: string
): Promise<IValidationError | number> => {

1.2.4 asyncValidateFile

Note: file type is Express.Multer.File Return an error if it is failed the validation, return file (as inputted data) if it passed the validation Refer: use asyncValidateFile

asyncValidateFile = async (
  schema: IFileSchema,
  data?: any,
  name?: string
): Promise<IValidationError | Express.Multer.File> => {

1.2.5 asyncValidateDate

Return an error if it is failed the validation, return Date (as inputted data or converted data) if it passed the validation Refer: use asyncValidateDate

Note: we convert to moment data for validation, so the format base on moment rules.

asyncValidateDate = async (
  schema: IDateSchema,
  data?: any,
  name?: string
): Promise<IValidationError | Date> => {

1.2.6 asyncValidateBoolean

Return an error if it is failed the validation, return Boolean (as inputted data or converted data) if it passed the validation Refer: use asyncValidateBoolean

asyncValidateBoolean = async (
  schema: IBooleanSchema,
  data?: any,
  name?: string
): Promise<IValidationError | boolean> => {

1.2.7 asyncValidateEnum

Return an error if it is failed the validation, return Enum Value (as inputted data) if it passed the validation Refer: use asyncValidateEnum

asyncValidateEnum = async (
  schema: IEnumSchema,
  data?: any,
  name?: string
): Promise<IValidationError | any> => {

1.2.8 asyncValidateArray

Return an error if it is failed the validation, return Array (as inputted data with corrected items ) if it passed the validation Refer: use asyncValidateArray

asyncValidateArray = async <T>(
  schema: IArraySchema<T>,
  data?: any,
  name?: string
): Promise<IValidationError | Array<T>> => {

1.2.9 asyncValidateObject

Return an error if it is failed the validation, return Object (as inputted data with corrected properties ) if it passed the validation Refer: use asyncValidateObject

asyncValidateObject = async <T = unknown>(
  schema: IObjectSchema<T>,
  data?: T,
  name?: string
): Promise<IValidationError | TypeOptional<T>> => {

2 Schemas

ISchemaGeneral is a common type, It can be understand as all schema has in the library.

export type ISchemaGeneral =  ISchemaCore | IBooleanSchema | IStringSchema | INumberSchema | IIntegerSchema | IFileSchema | IDateSchema | IEnumSchema | IArraySchema | IObjectSchema;

2.1 ISchemaCore.

Core schema with validation is extends IValidationCore

PropertyTypeRequiredDefaultDescription
typeenum: SchemaTypedefault objecttype of schema which we base on that to validate
propertyTypeClass name, Function object nameuse for validation an object class (type is object)
descriptionStringDescription for schema
validationObject: Instance of object extend IValidationCoreDetail the validation rules, custom the validation if need

2.2 IStringSchema.

String schema extend ISchemaCore with validation is IValidationString

2.3 INumberSchema.

Number schema extend ISchemaCore with validation is IValidationNumber

2.4 IIntegerSchema.

Integer schema extend ISchemaCore with validation is IValidationInteger

2.5 IFileSchema.

File schema extend ISchemaCore with validation is IValidationFile

2.6 IDateSchema.

Array schema extend ISchemaCore with validation is IValidationDate with extra properties:

PropertyTypeRequiredDefaultDescription
formatstringmoment format
convertToDateMoment(originData: any) => Momentconvert data to moment date for validation
let dateSchema: IDateSchema = {
  type: 'date',
  dateFormat: 'YYYY-MM-DD',
  validation: {...},
};
// **Note: when you use convertToDateMoment, dateFormat will be ignored.**
dateSchema = {
  type: 'date',
  convertToDateMoment: (date) => moment(date, 'YYYY-MM-DD'),
  validation: {...},
};

// convert to local timezone
dateSchema = {
  type: 'date',
  convertToDateMoment: (data: string) => {
    const currentOffset = moment().utcOffset();
    return moment.utc(data, 'YYYY-MM-DD').utcOffset(currentOffset);
  },
  validation: {...},
};

2.6 IEnumSchema.

Enum schema extend ISchemaCore with validation is IValidationEnum with extra properties:

PropertyTypeRequiredDefaultDescription
enumData{key: string:(string or number)}trueall data can have of enum
enumNamestringname of enum
enum TempEnum {
  active = 'active',
  inactive = 'inactive',
  normal = 'normal',
}
const enumSchema: IEnumSchema = {
  type: 'enum',
  enumData: getEnumData(TempEnum),
  enumName: 'TempEnum'
  validation: {...},
};

2.6 IArraySchema.

Array schema extend ISchemaCore with validation is IValidationArray with extra properties:

PropertyTypeRequiredDefaultDescription
itemSchemaISchemaGeneralschema use to verify(if needed) each item in array,

2.6 IObjectSchema.

Object schema extend ISchemaCore with validation is IValidationObject with extra properties:

PropertyTypeRequiredDefaultDescription
properties{key: ISchemaGeneral}each property will have a schema which use for validation it's value

#3 . Data

3.1 SchemaType

Types of schema which library support for validation:
'boolean' |'string' |'number' |'integer' |'enum' |'date' |'file' |'array' |'object'

3.2 IValidationCore

PropertyTypeRequiredDefaultDescription
isRequiredBooleanfalsemark for validation, item is required or not, default is false
validationFunctioncustom the added validation with data of item and item's path name

NOTE: validation: (data, pathName) => (IValidationError | undefined) | Promise<IValidationError | undefined>

3.3 IValidationString

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
formatRegex or 'isEmail'isEmail is a supported Regex
minLengthintegermin length of string for validation
maxLengthintegermax length of string for validation
byteLengthObjectcheck length of string in byte size
byteLength.minintegermin byte after convert
byteLength.maxintegermax byte after convert

3.4 IValidationNumber

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
minintegermin value for validation
maxintegermax value for validation

3.5 IValidationInteger

is the same with IValidationNumber

3.6 IValidationFile

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
minSizeintegermin size of file for validation
maxSizeintegermax size of file for validation
mineTypestringtype of file for validation

3.7 IValidationDate

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
minDatemin date for validation
maxDatemax date for validation

3.8 IValidationEnum

is extended IValidationNumber with no extra properties.

3.9 IValidationArray

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
minLengthintegermin length of array for validation
maxLengthintegermax length of array for validation

3.10 IValidationObject

is extended IValidationCore with extra properties:

PropertyTypeRequiredDefaultDescription
isNotAlowExtPropertiesbooleanallow the other properties which is not declared in object or not, true=> not alow, false (undefined) => allow
4.0.2

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.6.15

2 years ago

3.6.14

2 years ago

3.6.13

2 years ago

3.6.12

2 years ago

3.6.11

2 years ago

3.6.10

2 years ago

3.4.0

3 years ago

3.6.2

3 years ago

3.6.1

3 years ago

3.6.0

3 years ago

3.6.6

3 years ago

3.6.5

3 years ago

3.6.4

3 years ago

3.6.3

3 years ago

3.6.8

3 years ago

3.6.7

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.0

3 years ago

3.0.24

3 years ago

3.0.21

3 years ago

3.0.22

3 years ago

3.0.20

3 years ago

3.0.18

3 years ago

3.0.19

3 years ago

3.0.9

3 years ago

3.0.12

3 years ago

3.0.4

3 years ago

3.0.13

3 years ago

3.0.3

3 years ago

3.0.10

3 years ago

3.0.11

3 years ago

3.0.1

3 years ago

3.0.16

3 years ago

3.0.8

3 years ago

3.0.17

3 years ago

3.0.7

3 years ago

3.0.14

3 years ago

3.0.6

3 years ago

3.0.15

3 years ago

3.0.5

3 years ago

3.0.2

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago