# art-validation

> Validate object props.

Latest version **2.9.2** (published 2025-11-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install art-validation
pnpm add art-validation
yarn add art-validation
bun add art-validation
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

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

## Facts

| | |
|---|---|
| Version | 2.9.2 |
| Published | 2025-11-07 |
| First published | 2017-07-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 158.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Shane Brinkman-Davis Delamore |
| Maintainers | shanebdavis |

## Links

- npm: https://www.npmjs.com/package/art-validation
- Repository: https://github.com/art-suite/art-suite-foundations
- Issues: https://github.com/art-suite/art-suite-foundations/issues
- npm.io page: https://npm.io/package/art-validation

## Dependencies (3)

- [art-class-system](https://npm.io/package/art-class-system.md) ^1.11.20
- [art-standard-lib](https://npm.io/package/art-standard-lib.md) ^1.74.4
- [art-communication-status](https://npm.io/package/art-communication-status.md) ^1.6.2

## Recent versions

- 2.9.2 (latest) — 2025-11-07
- 2.9.1 — 2024-11-27
- 2.9.0 — 2024-08-04
- 2.8.0 — 2024-07-25
- 2.7.1 — 2024-05-07
- 2.7.0 — 2024-04-19
- 2.6.1 — 2024-04-12
- 2.6.0 — 2024-04-12
- 2.5.0 — 2024-03-29
- 2.4.0 — 2024-03-29
- 2.3.0 — 2024-02-26
- 2.2.2 — 2024-02-22
- 2.2.1 — 2024-02-22
- 2.2.0 — 2024-02-13
- 2.1.3 — 2023-12-11
- … 28 more at https://npm.io/package/art-validation/versions

## README

# ArtValidator 2.0

Validate object properties.

### NOTES

- validators are evaluated before preprocessors
- preprocessors should NOT throw validation-related errors

### Example

Simplest: _"name", must be a string (or null or undefined)_

```coffeescript
validator = new Validator name: "string"
# or, verbose: new Validator name: fieldType: "string"

assert.true !!validator.validate name: "Alice"
assert.true !!validator.validate {} # name not required

assert.throws -> validator.validate name: 123   # not a string
```

Required: _"name", must be a string (and not null or undefined)_

```coffeescript
validator = new Validator name: "required string"
# or, verbose: new Validator name: required: true, fieldType: "string"

assert.true !!validator.validate name: "Alice"
assert.throws -> validator.validate {} # name required
```

Exclusive: _"name" is the only field allowed_

```coffeescript
validator = new Validator {name: "string"}, exclusive: true
# or, verbose: new Validator {name: fieldType: "string"}, exclusive: true

assert.true !!validator.validate name: "Alice"
assert.throws -> validator.validate name: "Alice" age: 123 # exclusive!
assert.throws -> validator.validate name: 123
```

Three different ways to express field-property sequences:

```coffeescript
new Validator age: required: "integer"  # only `required` and `present` can be expressed this way
new Validator age: "required integer"   # strings are broken up on word boundaries
new Validator age: ["required", "integer"]
```

### USAGE

```coffeescript
new Validator validatorFieldsProps, options

IN:
  validatorFieldsProps:
    plain object with zero or more field-validations defined:
      fieldName: fieldProps
  options:
    exclusive: true/false
      if true, only fields listed in validatorFieldsProps are allowed.

fieldProps:
  string or plainObject
  string: selects fieldProps from one of the standard @FieldTypes (see below)
  plainObject: (all fields are optional)

    validate: (v) -> true/false
      v is never null nor undefined
      whenever this field is included in an update OR create operation,
        validate() must return true
      NOTE: validate is evaluated BEFORE preprocess

    postValidate: (v) -> true/false
      v is never null nor undefined
      whenever this field is included in an update OR create operation,
        validate() must return true
      NOTE: validate is evaluated AFTER preprocess

    preprocess: (v1) -> v2
      v1 is never null nor undefined
      whenever this field is included in an update OR create operation,
        after validation succeeds,
        value = preprocess value
      NOTE: validate is evaluated BEFORE preprocess

    required: true/false/string
      if true/string
        when creating records, this field must be included
      if string
        fieldProps = merge fieldProps, FieldTypes[string]

    present: true/false
      if true
        when creating records, this field must be include and 'present' (see Art.Foundation.present)

    fieldType: string
      fieldProps = merge FieldTypes[string], fieldProps

    dataType: string
      sepecify which of the standard Json data-types this field contains
      This is not used by Validator itself, but is available for clients to reflect on field-types.
      Must be one of the values in: DataTypes

    instanceof: class
      in addition to passing validate(), if present, the value must also be an instance of the
      specified class

    default: value or function
      If field is not included in the object being validated, the default value will be used.
      If the default is a function, it will be invoked with f(fieldName, normalizedFieldProps)
      UNLESS your fieldProps.dataType is "function" in which case the default value is used as-is (see the "defaultFunction" option.

    defaultFunction: (fieldName, normalizedFieldProps) ->
      If your fieldProps.dataType is "function", you can use this to create the default function
      dynamically.
```

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