Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

scrubbr

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

scrubbr - npm Package Compare versions

Comparing version 0.0.1-alpha.1 to 0.0.1-alpha.2

example/README.md

3

package.json
{
"name": "scrubbr",
"version": "0.0.1-alpha.1",
"version": "0.0.1-alpha.2",
"description": "Serialize your JSON API data using your TypeScript as the schema.",
"repository": "https://github.com/jgillick/scrubbr",
"main": "dist/index.js",

@@ -6,0 +7,0 @@ "scripts": {

# Scrubbr
[![Tests](https://github.com/jgillick/scrubbr/actions/workflows/test.yml/badge.svg)](https://github.com/jgillick/scrubbr/actions)
[![npm version](https://img.shields.io/npm/v/scrubbr)](https://badge.fury.io/js/scrubbr)
<!-- [![downloads](https://img.shields.io/npm/dm/Scrubbr)](https://www.npmjs.com/package/scrubbr) -->
Serialize and sanitize JSON API data using your TypeScript as the schema.
![Simple Example](./example.png)
![Simple Example](https://github.com/jgillick/scrubbr/raw/main/example.png)

@@ -39,4 +42,5 @@ Serializing and sanitizing data sent from the webserver to the client shouldn't be hard. If you're already using TypeScript, you have everything you need. Scrubbr will use your TypeScript types to deeply transform and sanitize your data.

// Load the typescript file and convert it to a schema that will be used later.
// Performance note: this is a synchronous file load. Load early and cache to a shared variable.
// Convert the typescript file to a schema
// PERFORMANCE NOTE: this is a synchronous call.
// Load early and cache to a shared variable.
const scrubbr = new Scrubbr('./schema.ts');

@@ -47,3 +51,3 @@

// Serialize the data based on the PostList type defined in schema.ts
// Serialize the data based on the UserList type defined in schema.ts
return await scrubbr.serialize(data, 'UserList');

@@ -144,1 +148,32 @@ }

```
# Advanced Topics
## Validation
For the sake of performance and simplicity, scrubber does not perform a schema validation step (it outputs data, not validates). However, under the hood scrubbr converts TypeScript to JSONSchema (via the great [ts-json-schema-generator](https://www.npmjs.com/package/ts-json-schema-generator) package). So you can easily use [ajv](https://www.npmjs.com/package/ajv) to validate the serialized object.
```typescript
import Ajv from 'ajv';
import Scrubbr from 'scrubbr';
const scrubbr = new Scrubbr('./schema.ts');
async function main() {
// Serialize
const output = await scrubbr.serialize(data, 'UserList');
const jsonSchema = scrubbr.getSchema();
// Validate
const ajv = new Ajv();
const schemaValidator = ajv.compile(jsonSchema);
const isValid = schemaValidator(output);
if (!isValid) {
console.error(schemaValidator.errors);
}
}
```
# License
[MIT](https://github.com/ajv-validator/ajv/blob/HEAD/LICENSE)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc