Comparing version 0.0.0 to 0.1.0
@@ -1,2 +0,8 @@ | ||
import type { TOC } from './types.js'; | ||
import type { TOC, Entry, FileEntry, FileParentEntry, URLEntry, URLParentEntry, PatternEntry, ParentEntry } from './types.js'; | ||
import type { ValidationOptions } from 'simple-validators'; | ||
export declare function validateFileEntry(entry: any, opts: ValidationOptions): FileEntry | FileParentEntry | undefined; | ||
export declare function validateURLEntry(entry: any, opts: ValidationOptions): URLEntry | URLParentEntry | undefined; | ||
export declare function validatePatternEntry(entry: any, opts: ValidationOptions): PatternEntry | undefined; | ||
export declare function validateParentEntry(entry: any, opts: ValidationOptions): ParentEntry | undefined; | ||
export declare function validateEntry(entry: any, opts: ValidationOptions): Entry | undefined; | ||
/** | ||
@@ -7,3 +13,3 @@ * validate a MyST table of contents | ||
*/ | ||
export declare function validateTOC(toc: Record<string, unknown>): TOC; | ||
export declare function validateTOC(toc: any, opts: ValidationOptions): TOC | undefined; | ||
//# sourceMappingURL=toc.d.ts.map |
137
dist/toc.js
@@ -1,3 +0,124 @@ | ||
import schema from './schema.json'; | ||
import _Ajv from 'ajv'; | ||
import { defined, incrementOptions, validateBoolean, validateList, validateObjectKeys, validateObject, validateString, validationError, } from 'simple-validators'; | ||
const COMMON_ENTRY_KEYS = ['title', 'hidden', 'numbering', 'id', 'part', 'class']; | ||
function validateCommonEntry(entry, opts) { | ||
const output = {}; | ||
if (defined(entry.title)) { | ||
output.title = validateString(entry.title, incrementOptions('title', opts)); | ||
} | ||
if (defined(entry.hidden)) { | ||
output.hidden = validateBoolean(entry.hidden, incrementOptions('hidden', opts)); | ||
} | ||
if (defined(entry.numbering)) { | ||
output.numbering = validateString(entry.numbering, incrementOptions('numbering', opts)); | ||
} | ||
if (defined(entry.id)) { | ||
output.id = validateString(entry.id, incrementOptions('id', opts)); | ||
} | ||
if (defined(entry.part)) { | ||
output.part = validateString(entry.part, incrementOptions('part', opts)); | ||
} | ||
if (defined(entry.class)) { | ||
output.class = validateString(entry.class, incrementOptions('class', opts)); | ||
} | ||
return output; | ||
} | ||
export function validateFileEntry(entry, opts) { | ||
const intermediate = validateObjectKeys(entry, { | ||
required: ['file'], | ||
optional: [...COMMON_ENTRY_KEYS, 'children'], | ||
}, opts); | ||
if (!intermediate) { | ||
return undefined; | ||
} | ||
const file = validateString(intermediate.file, incrementOptions('file', opts)); | ||
if (!file) { | ||
return undefined; | ||
} | ||
const commonEntry = validateCommonEntry(intermediate, opts); | ||
let output = { file, ...commonEntry }; | ||
if (defined(entry.children)) { | ||
const children = validateList(intermediate.children, incrementOptions('children', opts), (item, ind) => validateEntry(item, incrementOptions(`children.${ind}`, opts))); | ||
output = { children, ...output }; | ||
} | ||
return output; | ||
} | ||
export function validateURLEntry(entry, opts) { | ||
const intermediate = validateObjectKeys(entry, { | ||
required: ['url'], | ||
optional: [...COMMON_ENTRY_KEYS, 'children'], | ||
}, opts); | ||
if (!intermediate) { | ||
return undefined; | ||
} | ||
const url = validateString(intermediate.url, incrementOptions('url', opts)); | ||
if (!url) { | ||
return undefined; | ||
} | ||
const commonEntry = validateCommonEntry(intermediate, opts); | ||
let output = { url, ...commonEntry }; | ||
if (defined(entry.children)) { | ||
const children = validateList(intermediate.children, incrementOptions('children', opts), (item, ind) => validateEntry(item, incrementOptions(`children.${ind}`, opts))); | ||
output = { children, ...output }; | ||
} | ||
return output; | ||
} | ||
export function validatePatternEntry(entry, opts) { | ||
const intermediate = validateObjectKeys(entry, { | ||
required: ['pattern'], | ||
optional: [...COMMON_ENTRY_KEYS, 'children'], | ||
}, opts); | ||
if (!intermediate) { | ||
return undefined; | ||
} | ||
const pattern = validateString(intermediate.pattern, incrementOptions('pattern', opts)); | ||
if (!pattern) { | ||
return undefined; | ||
} | ||
const commonEntry = validateCommonEntry(intermediate, opts); | ||
return { pattern, ...commonEntry }; | ||
} | ||
export function validateParentEntry(entry, opts) { | ||
const intermediate = validateObjectKeys(entry, { | ||
required: ['title', 'children'], | ||
optional: [...COMMON_ENTRY_KEYS], | ||
}, opts); | ||
if (!intermediate) { | ||
return undefined; | ||
} | ||
const title = validateString(intermediate.title, incrementOptions('title', opts)); | ||
if (!title) { | ||
return undefined; | ||
} | ||
const children = validateList(intermediate.children, incrementOptions('children', opts), (item, ind) => validateEntry(item, incrementOptions(`children.${ind}`, opts))); | ||
if (!children) { | ||
return undefined; | ||
} | ||
const commonEntry = validateCommonEntry(intermediate, opts); | ||
return { | ||
children, | ||
title, | ||
...commonEntry, | ||
}; | ||
} | ||
export function validateEntry(entry, opts) { | ||
const intermediate = validateObject(entry, opts); | ||
if (!intermediate) { | ||
return undefined; | ||
} | ||
if (defined(intermediate.file)) { | ||
return validateFileEntry(intermediate, opts); | ||
} | ||
else if (defined(intermediate.url)) { | ||
return validateURLEntry(intermediate, opts); | ||
} | ||
else if (defined(intermediate.pattern)) { | ||
return validatePatternEntry(intermediate, opts); | ||
} | ||
else if (defined(intermediate.title)) { | ||
return validateParentEntry(intermediate, opts); | ||
} | ||
else { | ||
return validationError("expected an entry with 'file', 'url', 'pattern', or 'title'", opts); | ||
} | ||
} | ||
/** | ||
@@ -8,12 +129,4 @@ * validate a MyST table of contents | ||
*/ | ||
export function validateTOC(toc) { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
const Ajv = _Ajv.default; | ||
const ajv = new Ajv(); | ||
const validate = ajv.compile(schema); | ||
if (!validate(toc)) { | ||
throw new Error(`The given contents do not form a valid TOC.`); | ||
} | ||
return toc; | ||
export function validateTOC(toc, opts) { | ||
return validateList(toc, opts, (item, ind) => validateEntry(item, incrementOptions(`${ind}`, opts))); | ||
} |
@@ -27,4 +27,9 @@ /** | ||
/** | ||
* Entry with a URL to an external URL | ||
* Entry with a path to a single document with or without the file extension, | ||
* and an array of children | ||
*/ | ||
export type FileParentEntry = FileEntry & Omit<ParentEntry, 'title'>; | ||
/** | ||
* Entry with a url to an external resource | ||
*/ | ||
export type URLEntry = { | ||
@@ -34,2 +39,7 @@ url: string; | ||
/** | ||
* Entry with a url to an external resource, | ||
* and an array of children | ||
*/ | ||
export type URLParentEntry = URLEntry & Omit<ParentEntry, 'title'>; | ||
/** | ||
* Entry representing several documents through a glob | ||
@@ -41,10 +51,6 @@ */ | ||
/** | ||
* Entry representing a single document | ||
*/ | ||
export type DocumentEntry = FileEntry | URLEntry; | ||
/** | ||
* All possible types of Entry | ||
*/ | ||
export type Entry = DocumentEntry | (DocumentEntry & Omit<ParentEntry, 'title'>) | PatternEntry | ParentEntry; | ||
export type Entry = FileEntry | URLEntry | FileParentEntry | URLParentEntry | PatternEntry | ParentEntry; | ||
export type TOC = Entry[]; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "myst-toc", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"sideEffects": false, | ||
@@ -23,10 +23,9 @@ "license": "MIT", | ||
"scripts": { | ||
"clean": "rimraf dist ./src/schema.json", | ||
"clean": "rimraf dist", | ||
"lint": "eslint \"src/**/!(*.spec).ts\" -c ./.eslintrc.cjs", | ||
"lint:format": "npx prettier --check \"src/**/*.ts\"", | ||
"test": "npm-run-all build:schema && vitest run", | ||
"test": "vitest run", | ||
"test:watch": "vitest watch", | ||
"build:esm": "tsc", | ||
"build:schema": "npx ts-json-schema-generator --path src/types.ts --type TOC -o ./src/schema.json", | ||
"build": "npm-run-all -s -l clean build:schema build:esm" | ||
"build": "npm-run-all -s -l clean build:esm" | ||
}, | ||
@@ -37,7 +36,4 @@ "bugs": { | ||
"dependencies": { | ||
"ajv": "^8.12.0" | ||
}, | ||
"devDependencies": { | ||
"ts-json-schema-generator": "^1.5.0" | ||
"simple-validators": "^1.0.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
0
11963
14
216
2
+ Addedsimple-validators@^1.0.4
+ Addedsimple-validators@1.1.0(transitive)
- Removedajv@^8.12.0
- Removedajv@8.17.1(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-uri@3.0.3(transitive)
- Removedjson-schema-traverse@1.0.0(transitive)
- Removedrequire-from-string@2.0.2(transitive)