Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kawasima/json-spec
JSON Spec is a tool for validation and generation of JSON data.
https://github.com/kawasima/json-spec
json json-generator json-validation
Last synced: 21 days ago
JSON representation
JSON Spec is a tool for validation and generation of JSON data.
- Host: GitHub
- URL: https://github.com/kawasima/json-spec
- Owner: kawasima
- License: epl-2.0
- Created: 2018-09-05T01:38:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T06:06:16.000Z (about 2 years ago)
- Last Synced: 2025-01-20T09:39:58.292Z (26 days ago)
- Topics: json, json-generator, json-validation
- Language: JavaScript
- Homepage:
- Size: 4.38 MB
- Stars: 26
- Watchers: 4
- Forks: 2
- Open Issues: 85
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
JSON Spec
===========JSON Spec is a tool for validation and generation of JSON data. This is a porting of a part of clojure.spec.
## Validate
Spec is defined as follows
```javascript
const s = require('@json-spec/core');
const bigEven = s.and(x => !isNaN(Number(x)), x => x%2 === 0, x => x > 1000);
```And you can validate using the spec
```javascript
s.isValid(bigEven, 'foo'); // false
s.isValid(bigEven, 10); // false
s.isValid(bigEven, 10000); // true
```The example of validating a JSON Object is as follows.
```javascript
const isString = x => typeof(x) === 'string';
const emailMatch = x => new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/).test(x);
const emailType = s.and(isString, emailMatch);const person = s.object({
first_name: isString,
last_name: isString,
email: emailType
});s.isValid(person, {
first_name: 'Yoshitaka',
last_name: 'Kawashima',
email: '[email protected]'
}); // true```
## Generate
JSON spec can also generate JSON data that satisfies the defined specification.
```javascript
const s = require('@json-spec/core');
const gen = require('@json-spec/core/gen');
const sp = require('@json-spec/spec-profiles');
const sb = require('@json-spec/spec-basic');const person = s.object({
required: {
firstName: sp.name({ size: 100 }),
lastName: sp.name({ size: 100 }),
birthDay: sp.birthDay,
postalCd: sp.postalCode_JP,
languages: s.array([
"C", "C++", "Java"
])
}
});gen.sample(s.gen(person))
```This code generates JSON data as follows.
```javascript
{ firstName: 'n6lB',
lastName: 'lvf',
birthDay: 1960-05-10T15:00:00.000Z,
postalCd: '4874565',
languages: [ 'C', 'C', 'C' ] },
{ firstName: '6',
lastName: 'j',
birthDay: 2014-11-14T15:00:00.000Z,
postalCd: '5244670',
languages: [ 'C', 'Java', 'C', 'C++', 'C++' ] },
{ firstName: '5RV',
lastName: 'swy',
birthDay: 1989-07-13T15:00:00.000Z,
postalCd: '6731807',
languages: [ 'C++', 'Java' ] },
{ firstName: 'ubiIf',
lastName: 't5I',
birthDay: 1998-11-29T15:00:00.000Z,
postalCd: '2768511',
languages: [ 'C++', 'C', 'Java', 'C++', 'C', 'C', 'Java' ] },
{ firstName: 'P',
lastName: '0335Nlbl',
birthDay: 2005-08-24T15:00:00.000Z,
postalCd: '6810354',
languages: [ 'C++', 'C++', 'Java', 'C', 'Java' ] },
{ firstName: '5uYLGk9j',
lastName: 'N',
birthDay: 1932-06-06T15:00:00.000Z,
postalCd: '2292116',
languages: [ 'Java', 'C++', 'Java', 'C', 'C++' ] }
```