npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bakku/validation

v4.0.2

Published

## 1.1 Install

Downloads

38

Readme

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

| Property | Type | Required | Default | Description | | :----------- | :----------------------------------------------------------------------- | -------- | ---------------- | ---------------------------------------------------------- | | type | enum: SchemaType | | default object | type of schema which we base on that to validate | | propertyType | Class name, Function object name | | | use for validation an object class (type is object) | | description | String | | | Description for schema | | validation | Object: Instance of object extend IValidationCore | | | Detail 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:

| Property | Type | Required | Default | Description | | :------------------ | :-------------------------- | -------- | ------- | ------------------------------------------ | | format | string | | | moment format | | convertToDateMoment | (originData: any) => Moment | | | convert 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:

| Property | Type | Required | Default | Description | | :------- | :--------------------------------- | -------- | ------- | ------------------------- | | enumData | {[key: string]:(string or number)} | true | | all data can have of enum | | enumName | string | | | name 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:

| Property | Type | Required | Default | Description | | :--------- | :--------------------------- | -------- | ------- | --------------------------------------------------- | | itemSchema | ISchemaGeneral | | | schema use to verify(if needed) each item in array, |

2.6 IObjectSchema.

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

| Property | Type | Required | Default | Description | | :--------- | :------------------------------------ | -------- | ------- | -------------------------------------------------------------------- | | 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

| Property | Type | Required | Default | Description | | :--------- | :------- | -------- | ------- | ------------------------------------------------------------------ | | isRequired | Boolean | | false | mark for validation, item is required or not, default is false | | validation | Function | | | custom 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:

| Property | Type | Required | Default | Description | | :------------- | :----------------- | -------- | ------- | ----------------------------------- | | format | Regex or 'isEmail' | | | isEmail is a supported Regex | | minLength | integer | | | min length of string for validation | | maxLength | integer | | | max length of string for validation | | byteLength | Object | | | check length of string in byte size | | byteLength.min | integer | | | min byte after convert | | byteLength.max | integer | | | max byte after convert |

3.4 IValidationNumber

is extended IValidationCore with extra properties:

| Property | Type | Required | Default | Description | | :------- | :------ | -------- | ------- | ------------------------ | | min | integer | | | min value for validation | | max | integer | | | max value for validation |

3.5 IValidationInteger

is the same with IValidationNumber

3.6 IValidationFile

is extended IValidationCore with extra properties:

| Property | Type | Required | Default | Description | | :------- | :------ | -------- | ------- | ------------------------------- | | minSize | integer | | | min size of file for validation | | maxSize | integer | | | max size of file for validation | | mineType | string | | | type of file for validation |

3.7 IValidationDate

is extended IValidationCore with extra properties:

| Property | Type | Required | Default | Description | | :------- | :--- | -------- | ------- | ----------------------- | | min | Date | | | min date for validation | | max | Date | | | max date for validation |

3.8 IValidationEnum

is extended IValidationNumber with no extra properties.

3.9 IValidationArray

is extended IValidationCore with extra properties:

| Property | Type | Required | Default | Description | | :-------- | :------ | -------- | ------- | ---------------------------------- | | minLength | integer | | | min length of array for validation | | maxLength | integer | | | max length of array for validation |

3.10 IValidationObject

is extended IValidationCore with extra properties:

| Property | Type | Required | Default | Description | | :--------------------- | :------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------- | | isNotAlowExtProperties | boolean | | | allow the other properties which is not declared in object or not, true=> not alow, false (undefined) => allow |