@stoplight/json-schema-sampler
Advanced tools
Comparing version 0.0.0 to 0.1.0
@@ -457,2 +457,4 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.JSONSchemaSampler = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
function sampleArray(schema) { | ||
var _schema$maxItems; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -462,6 +464,8 @@ var spec = arguments.length > 2 ? arguments[2] : undefined; | ||
var depth = context && context.depth || 1; | ||
var arrayLength = schema.minItems || 1; | ||
var arrayLength = Math.min((_schema$maxItems = schema.maxItems) !== null && _schema$maxItems !== void 0 ? _schema$maxItems : Infinity, schema.minItems || 1); // for the sake of simplicity, we're treating `contains` in a similar way to `items` | ||
if (Array.isArray(schema.items)) { | ||
arrayLength = Math.max(arrayLength, schema.items.length); | ||
var items = schema.items || schema.contains; | ||
if (Array.isArray(items)) { | ||
arrayLength = Math.max(arrayLength, items.length); | ||
} | ||
@@ -471,10 +475,10 @@ | ||
if (Array.isArray(schema.items)) { | ||
return schema.items[itemNumber] || {}; | ||
return items[itemNumber] || {}; | ||
} | ||
return schema.items || {}; | ||
return items || {}; | ||
}; | ||
var res = []; | ||
if (!schema.items) return res; | ||
if (!items) return res; | ||
@@ -660,2 +664,6 @@ for (var i = 0; i < arrayLength; i++) { | ||
function idnEmailSample() { | ||
return 'пользователь@пример.ру'; | ||
} | ||
function passwordSample(min, max) { | ||
@@ -672,4 +680,8 @@ var res = 'pa$$word'; | ||
function commonDateTimeSample(min, max, omitTime) { | ||
var res = (0, _utils.toRFCDateTime)(new Date('2019-08-24T14:15:22.123Z'), omitTime, false); | ||
function commonDateTimeSample(_ref) { | ||
var min = _ref.min, | ||
max = _ref.max, | ||
omitTime = _ref.omitTime, | ||
omitDate = _ref.omitDate; | ||
var res = (0, _utils.toRFCDateTime)(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate, false); | ||
@@ -688,9 +700,28 @@ if (res.length < min) { | ||
function dateTimeSample(min, max) { | ||
return commonDateTimeSample(min, max); | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
omitTime: false, | ||
omitDate: false | ||
}); | ||
} | ||
function dateSample(min, max) { | ||
return commonDateTimeSample(min, max, true); | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
omitTime: true, | ||
omitDate: false | ||
}); | ||
} | ||
function timeSample(min, max) { | ||
return commonDateTimeSample({ | ||
min: min, | ||
max: max, | ||
omitTime: false, | ||
omitDate: true | ||
}).slice(1); | ||
} | ||
function defaultSample(min, max) { | ||
@@ -718,2 +749,6 @@ var res = (0, _utils.ensureMinLength)('string', min); | ||
function idnHostnameSample() { | ||
return 'пример.ру'; | ||
} | ||
function uriSample() { | ||
@@ -723,2 +758,18 @@ return 'http://example.com'; | ||
function uriReferenceSample() { | ||
return '../dictionary'; | ||
} | ||
function uriTemplateSample() { | ||
return 'http://example.com/{endpoint}'; | ||
} | ||
function iriSample() { | ||
return 'http://пример.ру'; | ||
} | ||
function iriReferenceSample() { | ||
return '../словарь'; | ||
} | ||
function uuidSample(_min, _max, propertyName) { | ||
@@ -728,13 +779,41 @@ return (0, _utils.uuid)(propertyName || 'id'); | ||
function jsonPointerSample() { | ||
return '/json/pointer'; | ||
} | ||
function relativeJsonPointerSample() { | ||
return '1/relative/json/pointer'; | ||
} | ||
function regexSample() { | ||
return '/regex/'; | ||
} | ||
var stringFormats = { | ||
'email': emailSample, | ||
'idn-email': idnEmailSample, | ||
// https://tools.ietf.org/html/rfc6531#section-3.3 | ||
'password': passwordSample, | ||
'date-time': dateTimeSample, | ||
'date': dateSample, | ||
'time': timeSample, | ||
// full-time in https://tools.ietf.org/html/rfc3339#section-5.6 | ||
'ipv4': ipv4Sample, | ||
'ipv6': ipv6Sample, | ||
'hostname': hostnameSample, | ||
'idn-hostname': idnHostnameSample, | ||
// https://tools.ietf.org/html/rfc5890#section-2.3.2.3 | ||
'iri': iriSample, | ||
// https://tools.ietf.org/html/rfc3987 | ||
'iri-reference': iriReferenceSample, | ||
'uri': uriSample, | ||
'uri-reference': uriReferenceSample, | ||
// either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1 | ||
'uri-template': uriTemplateSample, | ||
'uuid': uuidSample, | ||
'default': defaultSample | ||
'default': defaultSample, | ||
'json-pointer': jsonPointerSample, | ||
'relative-json-pointer': relativeJsonPointerSample, | ||
// https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01 | ||
'regex': regexSample | ||
}; | ||
@@ -918,4 +997,4 @@ | ||
function toRFCDateTime(date, omitTime, milliseconds) { | ||
var res = date.getUTCFullYear() + '-' + pad(date.getUTCMonth() + 1) + '-' + pad(date.getUTCDate()); | ||
function toRFCDateTime(date, omitTime, omitDate, milliseconds) { | ||
var res = omitDate ? '' : date.getUTCFullYear() + '-' + pad(date.getUTCMonth() + 1) + '-' + pad(date.getUTCDate()); | ||
@@ -922,0 +1001,0 @@ if (!omitTime) { |
{ | ||
"name": "@stoplight/json-schema-sampler", | ||
"version": "0.0.0", | ||
"description": "Tool for generation samples based on JSON Schema Draft 6", | ||
"version": "0.1.0", | ||
"description": "Tool for generation samples based on JSON Schema Draft 7", | ||
"main": "dist/json-schema-sampler.js", | ||
"module": "src/json-schema-sampler.js", | ||
"types": "./src/types.d.ts", | ||
"scripts": { | ||
@@ -47,2 +48,4 @@ "test": "gulp", | ||
"@babel/register": "^7.7.0", | ||
"ajv": "^8.1.0", | ||
"ajv-formats": "^2.0.2", | ||
"babel-eslint": "^10.0.3", | ||
@@ -70,2 +73,3 @@ "babel-loader": "^8.0.6", | ||
"gulp-uglify": "^3.0.2", | ||
"it-each": "^0.4.0", | ||
"json-loader": "^0.5.7", | ||
@@ -91,4 +95,5 @@ "karma": "^4.4.1", | ||
"dependencies": { | ||
"@types/json-schema": "^7.0.7", | ||
"json-pointer": "^0.6.1" | ||
} | ||
} |
# @stoplight/json-schema-sampler | ||
It's a fork of [openapi-sampler](https://github.com/Redocly/openapi-sampler) by Redocly, with focus on supporting JSON Schema Draft 6. | ||
It's a fork of [openapi-sampler](https://github.com/Redocly/openapi-sampler) by Redocly, with focus on supporting JSON Schema Draft 7. | ||
Tool for generation samples based on JSON Schema Draft 6. | ||
Tool for generation samples based on JSON Schema Draft 7. | ||
@@ -13,14 +13,24 @@ ## Features | ||
- Uses `default`, `const`, `enum` and `examples` where possible | ||
- Full array support: supports `minItems`, and tuples (`items` as an array) | ||
- Good array support: supports `contains`, `minItems`, `maxItems`, and tuples (`items` as an array) | ||
- Supports `minLength`, `maxLength`, `min`, `max`, `exclusiveMinimum`, `exclusiveMaximum` | ||
- Supports the next `string` formats: | ||
- idn-email | ||
- password | ||
- date-time | ||
- date | ||
- time | ||
- ipv4 | ||
- ipv6 | ||
- hostname | ||
- idn-hostname | ||
- uri | ||
- uri-reference | ||
- uri-template | ||
- iri | ||
- iri-reference | ||
- uuid | ||
- json-pointer | ||
- relative-json-pointer | ||
- regex | ||
- Infers schema type automatically following same rules as [json-schema-faker](https://www.npmjs.com/package/json-schema-faker#inferred-types) | ||
@@ -48,3 +58,3 @@ - Support for `$ref` resolving | ||
- **schema** (_required_) - `object` | ||
A JSON Schema Draft 6 document. | ||
A JSON Schema Draft 7 document. | ||
- **options** (_optional_) - `object` | ||
@@ -51,0 +61,0 @@ Available options: |
@@ -5,5 +5,7 @@ import { traverse } from '../traverse'; | ||
let arrayLength = schema.minItems || 1; | ||
if (Array.isArray(schema.items)) { | ||
arrayLength = Math.max(arrayLength, schema.items.length); | ||
let arrayLength = Math.min(schema.maxItems ?? Infinity, schema.minItems || 1); | ||
// for the sake of simplicity, we're treating `contains` in a similar way to `items` | ||
const items = schema.items || schema.contains; | ||
if (Array.isArray(items)) { | ||
arrayLength = Math.max(arrayLength, items.length); | ||
} | ||
@@ -13,9 +15,9 @@ | ||
if (Array.isArray(schema.items)) { | ||
return schema.items[itemNumber] || {}; | ||
return items[itemNumber] || {}; | ||
} | ||
return schema.items || {}; | ||
return items || {}; | ||
}; | ||
let res = []; | ||
if (!schema.items) return res; | ||
if (!items) return res; | ||
@@ -22,0 +24,0 @@ for (let i = 0; i < arrayLength; i++) { |
@@ -11,2 +11,6 @@ 'use strict'; | ||
function idnEmailSample() { | ||
return 'пользователь@пример.ру'; | ||
} | ||
function passwordSample(min, max) { | ||
@@ -21,4 +25,4 @@ let res = 'pa$$word'; | ||
function commonDateTimeSample(min, max, omitTime) { | ||
let res = toRFCDateTime(new Date('2019-08-24T14:15:22.123Z'), omitTime, false); | ||
function commonDateTimeSample({ min, max, omitTime, omitDate }) { | ||
let res = toRFCDateTime(new Date('2019-08-24T14:15:22.123Z'), omitTime, omitDate, false); | ||
if (res.length < min) { | ||
@@ -34,9 +38,13 @@ console.warn(`Using minLength = ${min} is incorrect with format "date-time"`); | ||
function dateTimeSample(min, max) { | ||
return commonDateTimeSample(min, max); | ||
return commonDateTimeSample({ min, max, omitTime: false, omitDate: false }); | ||
} | ||
function dateSample(min, max) { | ||
return commonDateTimeSample(min, max, true); | ||
return commonDateTimeSample({ min, max, omitTime: true, omitDate: false }); | ||
} | ||
function timeSample(min, max) { | ||
return commonDateTimeSample({ min, max, omitTime: false, omitDate: true }).slice(1); | ||
} | ||
function defaultSample(min, max) { | ||
@@ -62,2 +70,6 @@ let res = ensureMinLength('string', min); | ||
function idnHostnameSample() { | ||
return 'пример.ру'; | ||
} | ||
function uriSample() { | ||
@@ -67,2 +79,18 @@ return 'http://example.com'; | ||
function uriReferenceSample() { | ||
return '../dictionary'; | ||
} | ||
function uriTemplateSample() { | ||
return 'http://example.com/{endpoint}'; | ||
} | ||
function iriSample() { | ||
return 'http://пример.ру'; | ||
} | ||
function iriReferenceSample() { | ||
return '../словарь'; | ||
} | ||
function uuidSample(_min, _max, propertyName) { | ||
@@ -72,13 +100,35 @@ return uuid(propertyName || 'id'); | ||
function jsonPointerSample() { | ||
return '/json/pointer'; | ||
} | ||
function relativeJsonPointerSample() { | ||
return '1/relative/json/pointer'; | ||
} | ||
function regexSample() { | ||
return '/regex/'; | ||
} | ||
const stringFormats = { | ||
'email': emailSample, | ||
'idn-email': idnEmailSample, // https://tools.ietf.org/html/rfc6531#section-3.3 | ||
'password': passwordSample, | ||
'date-time': dateTimeSample, | ||
'date': dateSample, | ||
'time': timeSample, // full-time in https://tools.ietf.org/html/rfc3339#section-5.6 | ||
'ipv4': ipv4Sample, | ||
'ipv6': ipv6Sample, | ||
'hostname': hostnameSample, | ||
'idn-hostname': idnHostnameSample, // https://tools.ietf.org/html/rfc5890#section-2.3.2.3 | ||
'iri': iriSample, // https://tools.ietf.org/html/rfc3987 | ||
'iri-reference': iriReferenceSample, | ||
'uri': uriSample, | ||
'uri-reference': uriReferenceSample, // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1 | ||
'uri-template': uriTemplateSample, | ||
'uuid': uuidSample, | ||
'default': defaultSample | ||
'default': defaultSample, | ||
'json-pointer': jsonPointerSample, | ||
'relative-json-pointer': relativeJsonPointerSample, // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01 | ||
'regex': regexSample, | ||
}; | ||
@@ -85,0 +135,0 @@ |
@@ -10,6 +10,6 @@ 'use strict'; | ||
export function toRFCDateTime(date, omitTime, milliseconds) { | ||
var res = date.getUTCFullYear() + | ||
export function toRFCDateTime(date, omitTime, omitDate, milliseconds) { | ||
var res = omitDate ? '' : (date.getUTCFullYear() + | ||
'-' + pad(date.getUTCMonth() + 1) + | ||
'-' + pad(date.getUTCDate()); | ||
'-' + pad(date.getUTCDate())); | ||
if (!omitTime) { | ||
@@ -96,2 +96,2 @@ res += 'T' + pad(date.getUTCHours()) + | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55162
16
1358
83
2
46
+ Added@types/json-schema@^7.0.7
+ Added@types/json-schema@7.0.15(transitive)