# custom-validates

> this is a simple ts-validates

Latest version **2.2.5** (published 2022-05-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install custom-validates
pnpm add custom-validates
yarn add custom-validates
bun add custom-validates
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.5 |
| Published | 2022-05-13 |
| First published | 2022-02-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | hualazimi |
| Maintainers | hualazimi |
| Keywords | typescript, reflect-metadata |

## Links

- npm: https://www.npmjs.com/package/custom-validates
- npm.io page: https://npm.io/package/custom-validates

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.2.5 (latest) — 2022-05-13
- 2.2.4 — 2022-05-13
- 2.2.3 — 2022-05-12
- 2.2.2 — 2022-05-11
- 2.2.1 — 2022-05-11
- 2.1.3 — 2022-05-06
- 2.1.2 — 2022-02-25
- 2.1.1 — 2022-02-10
- 2.1.0 — 2022-02-10
- 2.0.8 — 2022-02-10
- 2.0.7 — 2022-02-09
- 2.0.6 — 2022-02-09
- 2.0.5 — 2022-02-09
- 2.0.4 — 2022-02-09
- 2.0.3 — 2022-02-09
- … 4 more at https://npm.io/package/custom-validates/versions

## README

## typescript simple validation

### abstract

config:

```typescript
// tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

```

WARN: This package is for learning only and cannot be used for production!!!

### update

Please use version >= 2.2.2

Fix

```typescript
class TestValidate {
  private readonly name = 'joy'
  @ValidateParams()
  async getName(person: ValidatePerson) {
    console.log(this.name) // fix this  result: 'joy'
    return 1
  }
}
```

### use

Users create custom validation rules themselves

#### regular check

```typescript
import { IsNotEmpty, Length, Max, Min, Optional } from 'custom-validates'

class ValidateBaseInfo {
  @IsNotEmpty({ message: 'name is not empty' })
  name: string;

  @Min({ message: 'age is greater than [value]', value: 18 })
  @IsNotEmpty({ message: 'age is not empty' })
  age: number;

  @Length({ message: 'length is [value]', value: 18 })
  @Optional()
  certno?: string;
}
```

#### custom validation

```typescript
import { FactoryValid } from 'custom-validates'

function IsMail({ message, value }) {
  const fn = v => {
    if (v) return true; // verify mail
  }
  return FactoryValid({ message, value }, fn)
}

class ValidateBaseInfo {
  @IsMail({ message: 'mail format error' })
  mail: string;
}

```

#### nest validation

```typescript
import { IsNotEmpty, Length, Max, Min, Optional } from 'custom-validates'

class ValidateBaseInfo {
  @Min({ message: 'age is greater than [value]', value: 18 })
  @IsNotEmpty({ message: 'age is not empty' })
  age: number;
  height: number;
}

class ValidateBodyParams {
  @Max({message: 'the id is less than [value]', value: 18})
  id: number;

  // deep: true; verify array
  // optional: true; optional params
  @Validate({ origin: ValidateBaseInfo, optional: true, deep: true })
  base?: ValidateBaseInfo[]
}
```

#### verify

```typescript
import { ValidateParams, IsNotEmpty, Min, Validate, ValidateObjectParmas } from 'custom-validates'

class ValidateBaseInfo {
  @Min({ message: 'age is greater than [value]', value: 18 })
  @IsNotEmpty({ message: 'age is not empty' })
  age: number;
  height: number;
}

class ValidateBodyParams {
  @Max({message: 'the id is less than [value]', value: 18})
  id: number;

  @Validate({ origin: ValidateBaseInfo, optional: true })
  base?: ValidateBaseInfo
}

const obj: ValidateBodyParams = {
  id: 15
}

const base: ValidateBaseInfo = {
  age: 17,
  height: 172
}

/** verify class-function */
class Test {
  @ValidateParams()
  async validate(obj: ValidateBodyParams, base: ValidateBaseInfo): Promise<any> {
    return 1
  }
}
/** class validation */
const test = new Test()

const value = test.validate(obj, base)
console.log('value', value)

/** object validation */
const objectValue = ValidateObjectParmas([ValidateBaseInfo], [base])
console.log('objectValue', objectValue)

```

---
_Source: https://npm.io/package/custom-validates · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
