Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kleversoncruz/json-schema-transformer
A JS library for applying schema-defined transformations and conversions to JSON data.
https://github.com/kleversoncruz/json-schema-transformer
format json json-schema scheme
Last synced: 28 days ago
JSON representation
A JS library for applying schema-defined transformations and conversions to JSON data.
- Host: GitHub
- URL: https://github.com/kleversoncruz/json-schema-transformer
- Owner: KleversonCruz
- Created: 2024-08-16T16:38:44.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-08-16T17:25:58.000Z (3 months ago)
- Last Synced: 2024-09-25T19:05:21.820Z (about 1 month ago)
- Topics: format, json, json-schema, scheme
- Language: TypeScript
- Homepage:
- Size: 111 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# json-schema-transformer
## Overview
`json-schema-transformer` is a library for enforcing schema-based transformations and on JSON data. It ensures data consistency by applying type conversions, custom formats, and default values according to the defined schema, making your JSON data compliant and well-structured.
## Getting Started
## Installation
To install json-schema-transformer:
```sh
npm install json-schema-transformer
```### Basic Usage
```js
const schema = {
type: 'object',
properties: {
invoiceNumber: { type: 'number' },
invoiceDate: { type: 'string', format: 'datetime' },
issuer: {
type: 'object',
properties: {
name: { type: 'string', transform: ['toLowerCase'] },
country: { type: 'string', default: 'USA' },
},
},
},
};const data = {
invoiceNumber: '123',
invoiceDate: '2024-08-15',
issuer: { name: 'Tech Innovators' },
};const output = {
invoiceNumber: 123,
invoiceDate: '2024-08-15T03:00:00.000Z',
issuer: { name: 'tech innovators', country: 'USA' },
};const jsonFormatter = new JsonFormatter();
jsonFormatter.execute(schema, data); // => output
```### Custom Formats
You can add and replace any default formats using addFormat method:
```js
const schema = {
type: 'object',
properties: {
code: { type: 'string', format: 'customFormat' },
},
};const data = {
code: 'foo',
};const output = {
code: 'G-FOO',
};const formatter = new JsonFormatter();
formatter.addFormat('customFormat', {
type: 'string',
format: (value) => `G-${value}`,
});jsonFormatter.execute(schema, data); // => output
```