Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agustinsrg/javascript-object-sanitizer
Object sanitizer for javascript and typescript.
https://github.com/agustinsrg/javascript-object-sanitizer
javascript json object sanitizer schema
Last synced: 24 days ago
JSON representation
Object sanitizer for javascript and typescript.
- Host: GitHub
- URL: https://github.com/agustinsrg/javascript-object-sanitizer
- Owner: AgustinSRG
- License: mit
- Created: 2021-06-18T10:05:58.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-02T14:43:53.000Z (3 months ago)
- Last Synced: 2024-11-09T07:48:36.193Z (about 1 month ago)
- Topics: javascript, json, object, sanitizer, schema
- Language: TypeScript
- Homepage: https://agustinsrg.github.io/javascript-object-sanitizer/
- Size: 807 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
```
## 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/)