@cypress/schema-tools
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -31,2 +31,6 @@ "use strict"; | ||
var debug = debug_1.default('schema-tools'); | ||
exports.getVersionedSchema = function (schemas) { return function (name) { | ||
name = utils.normalizeName(name); | ||
return schemas[name]; | ||
}; }; | ||
exports.getObjectSchema = function (schemas) { return function (schemaName) { return function (version) { | ||
@@ -33,0 +37,0 @@ schemaName = utils.normalizeName(schemaName); |
@@ -13,2 +13,4 @@ "use strict"; | ||
var lazy_ass_1 = __importDefault(require("lazy-ass")); | ||
var quote_1 = __importDefault(require("quote")); | ||
var ticks = quote_1.default({ quotes: '`' }); | ||
var title = [{ h1: 'Schemas' }]; | ||
@@ -27,2 +29,7 @@ var titleLink = [{ p: '[🔝](#schemas)' }]; | ||
var start = [{ h3: schemaName + "@" + version }]; | ||
if (schema.package) { | ||
start.push({ | ||
p: "Defined in " + ticks(schema.package), | ||
}); | ||
} | ||
if (schema.schema.description) { | ||
@@ -29,0 +36,0 @@ start.push({ p: schema.schema.description }); |
@@ -30,2 +30,13 @@ "use strict"; | ||
}; | ||
exports.setPackageName = function (schemas, packageName) { | ||
Object.keys(schemas).forEach(function (name) { | ||
Object.keys(schemas[name]).forEach(function (version) { | ||
var schema = schemas[name][version]; | ||
if (!schema.package) { | ||
schema.package = packageName; | ||
} | ||
}); | ||
}); | ||
return schemas; | ||
}; | ||
exports.combineSchemas = function () { | ||
@@ -32,0 +43,0 @@ var versioned = []; |
{ | ||
"name": "@cypress/schema-tools", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Validate, sanitize and document JSON schemas", | ||
@@ -47,3 +47,3 @@ "main": "dist", | ||
"@types/lodash.camelcase": "4.3.3", | ||
"@types/node": "9.6.6", | ||
"@types/node": "9.6.7", | ||
"@types/ramda": "0.25.24", | ||
@@ -60,3 +60,3 @@ "ava": "0.25.0", | ||
"terminal-banner": "1.1.0", | ||
"ts-node": "6.0.0", | ||
"ts-node": "6.0.1", | ||
"tslint": "5.9.1", | ||
@@ -63,0 +63,0 @@ "typescript": "2.8.3" |
@@ -132,3 +132,3 @@ # @cypress/schema-tools [](https://circleci.com/gh/cypress-io/schema-tools) [![renovate-app badge][renovate-badge]][renovate-app] | ||
* [assertSchema](#assertSchema) | ||
* [SchemaError](#schemaError) | ||
* [SchemaError](#schemaerror) | ||
@@ -162,2 +162,12 @@ TODO describe current API | ||
If you want to tell where a schema is coming from, you can set package name, which will add a note to the output Markdown | ||
```typescript | ||
import { setPackageName, documentSchemas } from '@cypress/schema-tools' | ||
import { schemas } from './schemas' | ||
setPackageName(schemas, 'my-schemas') | ||
console.log(documentSchemas(schemas, formats)) | ||
// each schema will have a note that it was defined in "my-schemas" | ||
``` | ||
### validate | ||
@@ -191,3 +201,3 @@ | ||
Checks a given object against schemas (and formats) and throws a [SchemaError](#schemaError) if the object violates the given schema. | ||
Checks a given object against schemas (and formats) and throws a [SchemaError](#schemaerror) if the object violates the given schema. | ||
@@ -194,0 +204,0 @@ ```js |
@@ -20,2 +20,9 @@ import { | ||
export const getVersionedSchema = (schemas: SchemaCollection) => ( | ||
name: string, | ||
) => { | ||
name = utils.normalizeName(name) | ||
return schemas[name] | ||
} | ||
/** | ||
@@ -22,0 +29,0 @@ * Returns object schema given a name and a version. Curried. |
@@ -20,3 +20,5 @@ // generates Markdown document with all schema information | ||
import la from 'lazy-ass' | ||
import quote from 'quote' | ||
const ticks = quote({ quotes: '`' }) | ||
const title = [{ h1: 'Schemas' }] | ||
@@ -51,2 +53,7 @@ const titleLink = [{ p: '[🔝](#schemas)' }] | ||
const start: any[] = [{ h3: `${schemaName}@${version}` }] | ||
if (schema.package) { | ||
start.push({ | ||
p: `Defined in ${ticks(schema.package)}`, | ||
}) | ||
} | ||
if (schema.schema.description) { | ||
@@ -53,0 +60,0 @@ start.push({ p: schema.schema.description }) |
@@ -76,2 +76,6 @@ /** | ||
example: PlainObject | ||
/** | ||
* Usually the name of the package this schema is defined in. | ||
*/ | ||
package?: string | ||
} | ||
@@ -78,0 +82,0 @@ |
@@ -47,2 +47,22 @@ import { | ||
/** | ||
* Sets name for each schema in the collection. | ||
* Note: mutates the input collection | ||
*/ | ||
export const setPackageName = ( | ||
schemas: SchemaCollection, | ||
packageName: string, | ||
) => { | ||
Object.keys(schemas).forEach(name => { | ||
Object.keys(schemas[name]).forEach(version => { | ||
const schema = schemas[name][version] | ||
if (!schema.package) { | ||
schema.package = packageName | ||
} | ||
}) | ||
}) | ||
// returns modified schemas just for convenience | ||
return schemas | ||
} | ||
/** | ||
* Combines multiple versioned schemas into single object | ||
@@ -49,0 +69,0 @@ * |
57411
1399
241