# @asanrom/javascript-object-sanitizer

> Object sanitizer for javascript.

Latest version **1.1.0** (published 2025-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @asanrom/javascript-object-sanitizer
pnpm add @asanrom/javascript-object-sanitizer
yarn add @asanrom/javascript-object-sanitizer
bun add @asanrom/javascript-object-sanitizer
```

## Health

**Score 55/100 (C)** — status: stable.

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2025-10-24 |
| First published | 2021-06-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 16.0.0 |
| Dependencies | 0 |
| Unpacked size | 61.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Agustin San Roman |
| Maintainers | asanrom |

## Links

- npm: https://www.npmjs.com/package/@asanrom/javascript-object-sanitizer
- Repository: https://github.com/AgustinSRG/javascript-object-sanitizer
- Issues: https://github.com/AgustinSRG/javascript-object-sanitizer/issues
- npm.io page: https://npm.io/package/@asanrom/javascript-object-sanitizer

## Recent versions

- 1.1.0 (latest) — 2025-10-24
- 1.0.2 — 2025-10-24
- 1.0.1 — 2025-10-24
- 1.0.0 — 2022-03-18
- 0.0.5 — 2021-10-22
- 0.0.4 — 2021-07-28
- 0.0.3 — 2021-06-29
- 0.0.2 — 2021-06-29
- 0.0.1 — 2021-06-28

## README

# Javascript Object Sanitizer

[![npm version](https://badge.fury.io/js/%40asanrom%2Fjavascript-object-sanitizer.svg)](https://badge.fury.io/js/%40asanrom%2Fjavascript-object-sanitizer)

Object sanitizer for javascript and typescript.

Forces the input to follow a pre-defined schema. Useful to check `application/json` bodies in web requests and responses from untrusted APIs.

## Installation

If you are using a npm managed project use:

```
npm install @asanrom/javascript-object-sanitizer
```

If you are using it in the browser, download the minified file from the [Releases](https://github.com/AgustinSRG/javascript-object-sanitizer/tags) section and import it to your html:

```html
<script type="text/javascript" src="/path/to/javascript-object-sanitizer.js"></script>
```

## Usage

Node:

```ts
import { ObjectSchema } from '@asanrom/javascript-object-sanitizer';

// With require: const ObjectSchema = require('@asanrom/javascript-object-sanitizer').ObjectSchema;

// Example schema or complex object
const schema = ObjectSchema.object({
    stringProperty: ObjectSchema.string().withDefaultValue(""),
    integerProperty: ObjectSchema.integer(),
    positiveNumber: ObjectSchema.number().withMin(0),
    array: ObjectSchema.array(ObjectSchema.object({
        arrayItemProp1: ObjectSchema.boolean(),
    })),
    optionalProperty: ObjectSchema.optional(ObjectSchema.number()),
});

// Test user input
schema.test(userInput); // Returns true or false

// Sanitize
const sanitized = schema.sanitize(userInput); // Forces user input to follow the schema
```

Browser:

```js
window.ObjectSchema = ObjectSanitizer.ObjectSchema;

// Example schema or complex object
const schema = ObjectSchema.object({
    stringProperty: ObjectSchema.string().withDefaultValue(""),
    integerProperty: ObjectSchema.integer(),
    positiveNumber: ObjectSchema.number().withMin(0),
    array: ObjectSchema.array(ObjectSchema.object({
        arrayItemProp1: ObjectSchema.boolean(),
    })),
    optionalProperty: ObjectSchema.optional(ObjectSchema.number()),
});

// Test user input
schema.test(userInput); // Returns true or false

// Sanitize
const sanitized = schema.sanitize(userInput); // Forces user input to follow the schema
```

## Documentation

 - [Library documentation (Auto-generated)](https://agustinsrg.github.io/javascript-object-sanitizer/)

---
_Source: https://npm.io/package/@asanrom/javascript-object-sanitizer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
