# field-test

> Custom object schema validation

Latest version **1.2.1** (published 2016-06-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install field-test
pnpm add field-test
yarn add field-test
bun add field-test
```

## 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.2.1 |
| Published | 2016-06-12 |
| First published | 2016-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | traducer |
| Maintainers | traducer |
| Keywords | object, validation, schema |

## Links

- npm: https://www.npmjs.com/package/field-test
- Repository: https://github.com/traducer/field-test
- Homepage: https://github.com/traducer/field-test#readme
- Issues: https://github.com/traducer/field-test/issues
- npm.io page: https://npm.io/package/field-test

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2016-06-12
- 1.2.0 — 2016-06-12
- 1.0.2 — 2016-06-10
- 1.0.1 — 2016-06-10
- 1.0.0 — 2016-06-10

## README

# field-test

This is a small object schema validation test builder. It does not come with a validation library built in. You simply provide a schema with test functions to test against object properties, and provide error messages if those tests fail.

Ideally, object properties should be strings but they can be anything you choose. 

[![NPM](https://nodei.co/npm/field-test.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/field-test/)

Quick Example
-------------

```js
/* works great for form validation */

const validate = require('field-test')

/* schema */
const schema = {
  username: [{
    test: function (str) { return str.length >=5 && str.length < 20 },
    message: 'username must contain 5 - 20 characters'
  }],
  email: [{
    test: function (str) { return /@/.test(str) },
    message: 'email must contain an @ character'
  },
  {
    test: function (str) { return /\.com/.test(str) },
    message: 'email must contain a .com extension'
  }]
}

validate({ username: 'java', email: 'javascript@rules.com' }, schema)

/* returns { username: ['username must contain 5 - 20 characters'] } */
```

Why?
----

Most of the validation and object schema validation libraries come with a lot of validation and sanitation methods that you might not need. Some of those libraries also don't have useful error messages (looking at you Joi). This allows you to write as many tests as you like with whatever error messages you want.

Each test also accepts an argument property if you need to pass a configuration object to your test. This is useful if you choose to use this with a validation library,

#### example

```js
/* import validator library from npm using es6 */

import { isLength, isEmail } from 'validator'
import validate from 'field-test'

/* using same example as above */
const schema = {
  username: [{
    test: isLength,
    argument: { min: 5, max: 20 },
    message: 'username must be between 5 - 20 characters'
  }],
  password: [{
    test: isEmail,
    message: 'email must be valid email format'
  }]
}

validate({ username: 'java', 'javascript@rules.com' }, schema)

/* returns { username: ['username must contain 5 - 20 characters'] } */
```

If all tests pass field-test will just return an empty object

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