Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maty21/jsonschema-validator-default
validate object and if it written correctly return an objectwith its missing defaults
https://github.com/maty21/jsonschema-validator-default
defaults javascript json-schema node-js validator
Last synced: 2 months ago
JSON representation
validate object and if it written correctly return an objectwith its missing defaults
- Host: GitHub
- URL: https://github.com/maty21/jsonschema-validator-default
- Owner: maty21
- License: mit
- Created: 2017-09-05T10:31:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-07-17T00:01:58.000Z (over 4 years ago)
- Last Synced: 2024-10-13T15:56:54.287Z (2 months ago)
- Topics: defaults, javascript, json-schema, node-js, validator
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## jsonschema-validate-default
[![Build Status](https://travis-ci.org/kube-HPC/jsonschema-validate-default.svg?branch=master)](https://travis-ci.org/kube-HPC/jsonschema-validate-default)
[![Coverage Status](https://coveralls.io/repos/github/kube-HPC/jsonschema-validate-default/badge.svg?branch=master)](https://coveralls.io/github/kube-HPC/jsonschema-validate-default?branch=master)just a tiny libary based on jsonschema libary that allows you to test if your object is valid and if so to combine it with your deafults data
### usage example
### lazy
```js
const validate = require('djsv');// create your schema
const json = {
"type": "object",
"properties": {
"id": {
"type": "string",
"default": "id",
"required": true
},
"age": {
"default": 30,
"type": "integer"
}
}
}let validObject = validate(json,{id:"maty"}) // validObject.instance => {id:"maty",age:30}
let validObject = validate(json,{id:"maty",age:20}) // validObject.instance => {id:"maty",age:20}
let validObject = validate(json,{age:20}) //=> {id:"maty",age:20} => error:{valid:false, errorDescription:"id is required"}
let validObject = validate(json,{id:123456}) //=> error:{valid:false, errorDescription:"id is not a string error "}
```### with schema initiation before
```jslet validateWithSchema = validate(json);
let obj= { id: "12345", info: { age: 6 } };
console.log(JSON.stringify(validateWithSchema(obj).instance));
```### ignore null option
ignorenull option allows you to set the default param in the return object although the value is defiend```js
validateWithSchema = validate(json);
console.log(JSON.stringify(validateWithSchema({ id: "12345", info: { age: 6, familyName: null } }).instance)) //=> {"id":"12345","info":{"age":6,"familyName":null}}
console.log(JSON.stringify(validateWithSchema({ id: "12345", info: { age: 6, familyName: null } }, { ignoreNull:true }).instance)) //=>{"id":"12345","info":{"age":6,"familyName":"unknown"}}```