@cypress/schema-tools
Advanced tools
Comparing version 1.8.1 to 1.9.0
@@ -26,3 +26,3 @@ "use strict"; | ||
lazy_ass_1.default(schema, 'cannot find schema', schemaName, version); | ||
var schemaDoc = utils_1.documentSchema(schema.schema); | ||
var schemaDoc = utils_1.documentSchema(schema.schema, schemas, formats); | ||
var start = [{ h3: schemaName + "@" + version }]; | ||
@@ -55,8 +55,7 @@ if (schema.package) { | ||
var fragments = ramda_1.flatten(__1.schemaNames(schemas).map(toDoc)); | ||
var anchor = function (s) { return ramda_1.toLower(s.replace(/[\.@]/g, '')); }; | ||
var schemaNameToTopLevelLink = function (schemaName) { | ||
return "[" + schemaName + "](#" + anchor(schemaName) + ")"; | ||
return "[" + schemaName + "](#" + utils_1.anchor(schemaName) + ")"; | ||
}; | ||
var schemaVersionLink = function (schemaName) { return function (version) { | ||
return "[" + version + "](#" + anchor(schemaName + version) + ")"; | ||
return "[" + version + "](#" + utils_1.anchor(schemaName + version) + ")"; | ||
}; }; | ||
@@ -63,0 +62,0 @@ var tocHeading = function (schemaName) { |
@@ -17,2 +17,9 @@ "use strict"; | ||
}; }; | ||
exports.anchor = function (s) { return ramda_1.toLower(s.replace(/[\.@]/g, '')); }; | ||
exports.anchorForSchema = function (s) { | ||
var schemaName = ramda_1.toLower(__1.normalizeName(s.schema.title)); | ||
var seeVersion = __1.semverToString(s.version); | ||
var nameAndVersion = schemaName + "@" + seeVersion; | ||
return exports.anchor(nameAndVersion); | ||
}; | ||
exports.enumToMarkdown = function (enumeration) { | ||
@@ -24,8 +31,20 @@ if (!enumeration) { | ||
}; | ||
exports.formatToMarkdown = function (value) { | ||
exports.formatToMarkdown = function (schemas, formats) { return function (value) { | ||
if (!value.format) { | ||
if (value.see) { | ||
return isSchemaName(value.see) | ||
? "[" + value.see + "](#" + ramda_1.toLower(__1.normalizeName(value.see)) + ")" | ||
: ticks(value.see); | ||
if (typeof value.see === 'string') { | ||
return schemas && isSchemaName(schemas)(value.see) | ||
? "[" + value.see + "](#" + ramda_1.toLower(__1.normalizeName(value.see)) + ")" | ||
: ticks(value.see); | ||
} | ||
else { | ||
var seeSchema = value.see; | ||
var schemaName = "" + seeSchema.schema.title; | ||
var seeVersion = __1.semverToString(seeSchema.version); | ||
var nameAndVersion = schemaName + "@" + seeVersion; | ||
var seeAnchor = exports.anchorForSchema(seeSchema); | ||
return schemas && isSchemaName(schemas)(schemaName) | ||
? "[" + nameAndVersion + "](#" + seeAnchor + ")" | ||
: ticks(nameAndVersion); | ||
} | ||
} | ||
@@ -36,7 +55,7 @@ else { | ||
} | ||
if (isCustomFormat(value.format)) { | ||
if (formats && isCustomFormat(formats)(value.format)) { | ||
return "[" + value.format + "](#formats)"; | ||
} | ||
return ticks(value.format); | ||
}; | ||
}; }; | ||
exports.findUsedColumns = function (headers, rows) { | ||
@@ -47,3 +66,3 @@ var isUsed = function (header) { return ramda_1.find(function (r) { return r[header]; }, rows); }; | ||
}; | ||
exports.documentProperties = function (properties, required) { | ||
exports.documentProperties = function (properties, required, schemas, formats) { | ||
if (required === void 0) { required = []; } | ||
@@ -60,3 +79,3 @@ var isRequired = function (name) { return required.indexOf(name) !== -1; }; | ||
required: isRequired(prop) ? exports.checkMark : exports.emptyMark, | ||
format: exports.formatToMarkdown(value), | ||
format: exports.formatToMarkdown(schemas, formats)(value), | ||
enum: exports.enumToMarkdown(value.enum), | ||
@@ -67,6 +86,6 @@ description: value.description ? value.description : exports.emptyMark, | ||
}; | ||
exports.documentSchema = function (schema) { | ||
exports.documentSchema = function (schema, schemas, formats) { | ||
var properties = schema.properties; | ||
if (properties) { | ||
var rows = exports.documentProperties(properties, schema.required); | ||
var rows = exports.documentProperties(properties, schema.required, schemas, formats); | ||
var headers = [ | ||
@@ -73,0 +92,0 @@ 'name', |
{ | ||
"name": "@cypress/schema-tools", | ||
"version": "1.8.1", | ||
"version": "1.9.0", | ||
"description": "Validate, sanitize and document JSON schemas", | ||
@@ -47,3 +47,3 @@ "main": "dist", | ||
"@types/lodash.camelcase": "4.3.3", | ||
"@types/node": "9.6.15", | ||
"@types/node": "9.6.16", | ||
"@types/ramda": "0.25.28", | ||
@@ -58,3 +58,3 @@ "ava": "0.25.0", | ||
"quote": "0.4.0", | ||
"semantic-release": "15.4.0", | ||
"semantic-release": "15.4.1", | ||
"terminal-banner": "1.1.0", | ||
@@ -61,0 +61,0 @@ "ts-node": "6.0.3", |
@@ -7,3 +7,3 @@ // generates Markdown document with all schema information | ||
import quote from 'quote' | ||
import { flatten, toLower } from 'ramda' | ||
import { flatten } from 'ramda' | ||
import { | ||
@@ -19,3 +19,3 @@ getExample, | ||
import { documentCustomFormats } from './doc-formats' | ||
import { documentSchema } from './utils' | ||
import { anchor, documentSchema } from './utils' | ||
@@ -49,3 +49,3 @@ const ticks = quote({ quotes: '`' }) | ||
const schemaDoc = documentSchema(schema.schema) | ||
const schemaDoc = documentSchema(schema.schema, schemas, formats) | ||
@@ -84,4 +84,2 @@ const start: any[] = [{ h3: `${schemaName}@${version}` }] | ||
const anchor = (s: string) => toLower(s.replace(/[\.@]/g, '')) | ||
const schemaNameToTopLevelLink = (schemaName: string) => | ||
@@ -88,0 +86,0 @@ `[${schemaName}](#${anchor(schemaName)})` |
import quote from 'quote' | ||
import { find, toLower } from 'ramda' | ||
import { normalizeName, schemaNames } from '..' | ||
import { normalizeName, schemaNames, semverToString } from '..' | ||
import { CustomFormats } from '../formats' | ||
import { JsonProperties, JsonSchema, SchemaCollection } from '../objects' | ||
import { | ||
JsonProperties, | ||
JsonProperty, | ||
JsonSchema, | ||
ObjectSchema, | ||
SchemaCollection, | ||
} from '../objects' | ||
@@ -26,2 +32,12 @@ const ticks = quote({ quotes: '`' }) | ||
// removes all characters to have a link | ||
export const anchor = (s: string) => toLower(s.replace(/[\.@]/g, '')) | ||
export const anchorForSchema = (s: ObjectSchema): string => { | ||
const schemaName = toLower(normalizeName(s.schema.title)) | ||
const seeVersion = semverToString(s.version) | ||
const nameAndVersion = `${schemaName}@${seeVersion}` | ||
return anchor(nameAndVersion) | ||
} | ||
export const enumToMarkdown = enumeration => { | ||
@@ -34,8 +50,23 @@ if (!enumeration) { | ||
export const formatToMarkdown = value => { | ||
export const formatToMarkdown = ( | ||
schemas?: SchemaCollection, | ||
formats?: CustomFormats, | ||
) => (value: JsonProperty): string => { | ||
if (!value.format) { | ||
if (value.see) { | ||
return isSchemaName(value.see) | ||
? `[${value.see}](#${toLower(normalizeName(value.see))})` | ||
: ticks(value.see) | ||
if (typeof value.see === 'string') { | ||
// try finding schema by name | ||
return schemas && isSchemaName(schemas)(value.see) | ||
? `[${value.see}](#${toLower(normalizeName(value.see))})` | ||
: ticks(value.see) | ||
} else { | ||
const seeSchema: ObjectSchema = value.see | ||
const schemaName = `${seeSchema.schema.title}` | ||
const seeVersion = semverToString(seeSchema.version) | ||
const nameAndVersion = `${schemaName}@${seeVersion}` | ||
const seeAnchor = anchorForSchema(seeSchema) | ||
return schemas && isSchemaName(schemas)(schemaName) | ||
? `[${nameAndVersion}](#${seeAnchor})` | ||
: ticks(nameAndVersion) | ||
} | ||
} else { | ||
@@ -46,3 +77,4 @@ return emptyMark | ||
if (isCustomFormat(value.format)) { | ||
if (formats && isCustomFormat(formats)(value.format)) { | ||
// point at the formats section | ||
return `[${value.format}](#formats)` | ||
@@ -72,2 +104,4 @@ } | ||
required: string[] = [], | ||
schemas?: SchemaCollection, | ||
formats?: CustomFormats, | ||
): PropertyDescription[] => { | ||
@@ -80,3 +114,3 @@ const isRequired = name => required.indexOf(name) !== -1 | ||
.map(prop => { | ||
const value = properties[prop] | ||
const value: JsonProperty = properties[prop] | ||
return { | ||
@@ -86,3 +120,3 @@ name: ticks(prop), | ||
required: isRequired(prop) ? checkMark : emptyMark, | ||
format: formatToMarkdown(value), | ||
format: formatToMarkdown(schemas, formats)(value), | ||
enum: enumToMarkdown(value.enum), | ||
@@ -94,3 +128,7 @@ description: value.description ? value.description : emptyMark, | ||
export const documentSchema = (schema: JsonSchema) => { | ||
export const documentSchema = ( | ||
schema: JsonSchema, | ||
schemas?: SchemaCollection, | ||
formats?: CustomFormats, | ||
) => { | ||
const properties = schema.properties | ||
@@ -102,2 +140,4 @@ | ||
schema.required, | ||
schemas, | ||
formats, | ||
) | ||
@@ -104,0 +144,0 @@ const headers = [ |
@@ -48,3 +48,3 @@ /** | ||
items?: JsonProperty | ||
see?: string | ||
see?: string | ObjectSchema | ||
title?: string | ||
@@ -51,0 +51,0 @@ patternProperties?: object |
71003
1741