# Installation

{% code fullWidth="false" %}

```bash
npm install @c4code/valid8r
```

{% endcode %}

Installing Valid8r is quick and simple. Just run **`npm install valid8r`** to get started, and you're ready to validate your fields with minimal setup.

{% hint style="info" %}
Valid8r supports Typescript type definitions. Head to the [Types](https://c4code.gitbook.io/valid8r/validators) section to learn more.
{% endhint %}

### Import

To use Valid8r in your project, simply import it with the following line

```typescript
import valid8r from '@c4code/valid8r';
```

That it! Once imported, you can start using its validation functions right away.

### Basic Usage

Here's how to use Valid8r for basic field validation

{% tabs %}
{% tab title="Typescript" %}

```typescript
import valid8r from '@c4code/valid8r';

// Your input's value
const input: string = "Jay Carlos";
const [isValid, errors] = valid8r.name(input);

console.log(isValid); // true

const input2: string = "M4rk Black";
valid8r.name(input); // throws NameValidationFailed error
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@c4code/valid8r';

// Your input's value
const input = "Jay Carlos";
const [isValid, errors] = valid8r.name(input);

console.log(isValid); // true

const input2 = "M4rk Black";
valid8r.name(input); // throws NameValidationFailed error
```

{% endtab %}
{% endtabs %}

The functions return `true` if the input is valid, and `false` if it is not. If the function returns `false`, the errors `Array` will contain all the errors that was given by the input.

### Safe Handling

Here's how to use Valid8r for basic field validation using Safe error throwing

{% tabs %}
{% tab title="Typescript" %}

```typescript
import valid8r from '@c4code/valid8r';

// Your input's value
const input: string = "Jay Carlos";
const [isValid, errors] = valid8r.name(input, { safe: true });

console.log(isValid); // if { safe: false }, this will always return true

const input2: string = "M4rk Black";
const [valid, errors2] = valid8r.name(input);

if (!valid) {
    console.log(errors2);
    // [{ "noSpChars": "Name should not contain any special characters." }]
}
```

{% endtab %}

{% tab title="Javascript" %}

```javascript
import valid8r from '@c4code/valid8r';

// Your input's value
const input = "Jay Carlos";
const [isValid, errors] = valid8r.name(input, { safe: true });

console.log(isValid); // if { safe: false }, this will always return true

const input2 = "M4rk Black";
const [valid, errors2] = valid8r.name(input);

if (!valid) {
    console.log(errors2);
    // [{ "noSpChars": "Name should not contain any special characters." }]
}
```

{% endtab %}
{% endtabs %}

`{ safe: true }` will ensure that all the errors will be returned instead of throwing them, if set to false it will throw the first error by default if not configured otherwise.

{% hint style="info" %}
Safe should only be set to true when you are manually handling errors.
{% endhint %}
