Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/runk/schema-casting
JSON Schema type casting and normalization
https://github.com/runk/schema-casting
Last synced: 3 months ago
JSON representation
JSON Schema type casting and normalization
- Host: GitHub
- URL: https://github.com/runk/schema-casting
- Owner: runk
- Created: 2018-01-29T02:56:07.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-22T00:00:26.000Z (over 1 year ago)
- Last Synced: 2024-10-01T05:01:28.822Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 52.7 KB
- Stars: 8
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
schema-casting [![Build Status](https://travis-ci.org/runk/schema-casting.png)](https://travis-ci.org/runk/schema-casting)
========Tiny tool aimed to type cast raw data based on provided json schema.
## Installation
```shell
npm i schema-casting
```## Usage
```javascript
const sc = require('schema-casting');const schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"a": { "required": true, "type": "integer" },
"b": { "required": true, "type": "boolean" },
"c": { "required": true, "type": "number" },
"d": {
"required": true,
"type": "object",
"properties": {
"e": {
"required": true,
"type": "number",
}
}
}
},
"required": true,
"additionalProperties": false
};const input = {
a: '1',
b: 'true',
c: '2.3',
d: { e: '4.56' },
};const output = sc(schema, input);
console.log(output);
// { a: 1, b: true, c: 2.3, d: { e: 4.56 } }
```## License
MIT