# sm-form-validation

> Simple form validation

Latest version **1.3.2** (published 2020-05-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install sm-form-validation
pnpm add sm-form-validation
yarn add sm-form-validation
bun add sm-form-validation
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2020-05-21 |
| First published | 2020-05-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | smolskig |
| Maintainers | smolskig |
| Keywords | front-end, javascript, form-validation, validation, value, validator, form, JS, node, js, validação, formulario |

## Links

- npm: https://www.npmjs.com/package/sm-form-validation
- Repository: https://github.com/smolskig/sm-form-validation
- Homepage: https://github.com/smolskig/sm-form-validation/blob/master/readme.md
- Issues: https://github.com/smolskig/sm-form-validation/issues
- npm.io page: https://npm.io/package/sm-form-validation

## Dependencies (1)

- [cpf_cnpj](https://npm.io/package/cpf_cnpj.md) ^0.2.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.3.2 (latest) — 2020-05-21
- 1.3.1 — 2020-05-21
- 1.3.0 — 2020-05-21
- 1.2.0 — 2020-05-21
- 1.1.0 — 2020-05-12
- 1.0.0 — 2020-05-10

## README

# sm-form-validation

A simple JS form Validation, for projects using Node.

## Installation

Install with NPM.

```bash
npm install sm-form-validation
```

## Simple Usage

```javascript
const { validate, required, email } = require('sm-form-validation') // import the validate function and the validations

validate( //Call validate function
     [
         {name: [ required('Fill the name') ]}, // you can pass a custom error message
         {email: [ email() ]}, // or just don't pass anything (Default return in English)
     ]
 )
     .then(() => { // success promise don't return any value
         console.log('success')
     })
     .catch((err) => { // error promise return an array with all error occurrences
         console.log(err)//array
     })
```

## The Validation Function
It receives an array of objects, that contains the value to validate and a validation array.<br/>
Then return a Promise that return empty when success or a array when has an error
```javascript
validate( [ { value : [ validation array ] } ] )
  .then(()=>{})
  .catch((err)=>{ console.log(err)})
```

## The Error Return
When the validation gets error, it return an Array with all the occurrences.<br/>
***This code:***
```javascript
validate(
    [
        {'': [required()]}, // empty value, validation Required()
    ]
)
    .then(() => {
        console.log('success')
    })
    .catch(err => {
        console.log(err)
    })
```
***Will return this Array:***
```javascript
[
  {
    value: '', // Value to validate
    index: 0,  // index on value array
    validation: [Function: validateRequired], // Validation Type
    status: 'The field is required' // The default error message
  }
]
```

## Custom Error Messages
For each validation, you can pass a custom error message as a param in the function.
```javascript
validate(
  [
    {'email@email': [ email('PUT AN VALID EMAIL PLS') ]}, // to single validations, just pass an string as a parameter
    {5 : [ minmax(4,10,['Minimum value not reached', 'Maximum value reached']) } // to minmax validation, you have to pass the messages on an array
  ]
 )
```

## Condition
For each object value, you can pass a condition to set if the validation gonna run or not as a second Key, called **condition**.<br/>
As a value of condition you have to pass an object with the **values** that you want to compare on the respectively order.
```javascript
let usingEmail = true // this variable gonna be the trigger to validate or not the email field
validate(
    [
        {'contatosmolski@gmail': [email('Wrong email')],condition: {[usingEmail]: true}}, // the sintax to condition is ' condition:{ firstValue : secondValue } '
    ]
)
```
In the code below we have two examples:<br/>
***The email gonna validate and throw an error:*** <br/>
***The name will not start validation:***
```javascript
let usingEmail = true // this variable gonna be the trigger to validate or not the email field
let usingName = false // this variable gonna be the trigger to validate or not the name field
validate(
    [
        {'contatosmolski@gmail': [email('Wrong email')],condition: {[usingEmail]: true}}, // passing a wrong email
        {'': [required('The name field is required')], condition: {[usingName]: true}} // passing a empty name (normaly it gonna return an error)
    ]
)
    .then(() => {
        console.log('success')
    })
    .catch(err => {
        console.log(err)
    })
```
**The Return**
```javascript
  [
    {
      value: 'contatosmolski@gmail', // it only validates the email
      index: 0,
      validation: [Function: validateEmail],
      status: 'Wrong email'
    }
  ]
```

## Validations
Name     |    Sintax  | Description
-------- | -----------|----------------------------------------------------------
Required | required( ' Custom Message ' ) | Validate the length of the value or if value is undefined or null
Custom   | custom( customValue, ' Custom Message ' ) | Validate a custom passed value
Email    | email( ' Custom Message ' )    | validate a string by an email valid Regex
Minmax   | minmax( minVal ,maxVal ,[ 'Min Custom Message', 'max Custom Message' ] ) | Validate a value by the min and the max value passed  
Cpf      | cpf( 'Custom Message' )| Validate a document by a valid Regex
Cnpj     | cnpj('Custom Message' )| Validate a document by a valid Regex

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