Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gastonpereyra/struct-prototype
Prototype to create a JS struct check library
https://github.com/gastonpereyra/struct-prototype
node prototype schema validator
Last synced: 8 days ago
JSON representation
Prototype to create a JS struct check library
- Host: GitHub
- URL: https://github.com/gastonpereyra/struct-prototype
- Owner: gastonpereyra
- License: mit
- Created: 2021-03-07T23:53:35.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-11T22:52:52.000Z (about 3 years ago)
- Last Synced: 2024-10-30T22:27:03.064Z (16 days ago)
- Topics: node, prototype, schema, validator
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/struct-prototype
- Size: 102 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Struct Prototype
Prototype to create a JS struct check library.## Code Quality Status
![Build Status](https://github.com/gastonpereyra/struct-prototype/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://img.shields.io/coveralls/github/gastonpereyra/struct-prototype/master.svg)](https://coveralls.io/r/gastonpereyra/struct-prototype?branch=master)---
## Description
It's a function to check a simple data structure.## Method
* `structPrototype(STRUCTURE|TYPE)(DATA): DATA|ERROR`
### Parameters
#### TYPE
The primitive **type** you want to check. In all cases must be pass as in an `string`
Supported:
* `'string'`
* `'number'`
* `'boolean'`
* `'object'`
* `'string'`
* `'array'`
* `'null'`#### STRUCTURE
You can define an data structure using a little more complex schema.
* **Array**: `[TYPE]`, check every element of the array to match that **type**
* **Object**: `{ [fieldName]: TYPE }`: check if every field define match that the **type** declared
* **Function**: ``(data) => true|false`: you can pass a function callback to check whatever you want, **must return** `true` or `false`#### DATA
Represents the values you want to check
### Response
#### DATA
If the check succes, returns the values.
#### ERROR
If the check fails, throws an Error with an message explaining what happened
## Usage
* Simple use
```js
const { structPrototype } = require('struct-prototype');structPrototype('string')('It is fine');
/*
output: 'It is fine'
*/structPrototype('number')('It is fine');
/*
output: Error. '"It is fine" is not number type'
*/
```
* Array structure```js
const { structPrototype } = require('struct-prototype');structPrototype(['string'])(['It is fine']);
/*
output: ['It is fine']
*/structPrototype(['number'])(['It is fine']);
/*
output: Error. '"It is fine" is not number type'
*/structPrototype(['Boolean'])('It is fine');
/*
output: Error. 'Data Structure is not an array'
*/```
* Object structure```js
const { structPrototype } = require('struct-prototype');const myDataStructure = {
name: 'string',
age: 'number',
isAdmin: 'Boolean'
}structPrototype(myDataStructure)({
name: 'Gastón',
age: 34,
isAdmin: true
});/*
output: {
name: 'Gastón',
age: 34,
isAdmin: true
}
*/structPrototype(myDataStructure)({
name: 'Gastón',
age: '34',
isAdmin: true
});/*
output: Error. '"34" is not number type'
*/structPrototype(myDataStructure)('It is fine');
/*
output: Error. 'Data Structure is not an object'
*/
```
* Custom Function```js
const { structPrototype } = require('struct-prototype');const isOverThirty = ({ age }) => age > 30;
structPrototype(isOverThirty)({
name: 'Gastón',
age: '34',
isAdmin: true
});/*
output: {
name: 'Gastón',
age: 34,
isAdmin: true
}
*/structPrototype(isOverThirty)({
name: 'Gastón',
age: 23,
isAdmin: true
});/*
output: Error. '"23" is not custom type'
*/structPrototype(myDataStructure)('It is fine');
/*
output: Error. '"It is fine" is not custom type'
*/
```