Comparing version 0.0.1-alpha.1 to 0.0.1-alpha.2
{ | ||
"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) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
271467
40
177