Split compute from report

This commit is contained in:
Tony Garnock-Jones 2021-05-24 10:16:00 +02:00
parent c4bfc0eefc
commit 9bdfc4c3ab
2 changed files with 10 additions and 5 deletions

View File

@ -2,14 +2,16 @@ import { typeFor, typeForIntersection } from './gentype';
import { ANY_TYPE, SimpleType } from './type'; import { ANY_TYPE, SimpleType } from './type';
import * as M from './meta'; import * as M from './meta';
export function checkSchema(schema: M.Schema): M.Schema { export function checkSchema(schema: M.Schema): (
{ ok: true, schema: M.Schema } | { ok: false, problems: Array<string> })
{
const checker = new Checker(); const checker = new Checker();
schema.definitions.forEach(checker.checkDefinition.bind(checker)); schema.definitions.forEach(checker.checkDefinition.bind(checker));
if (checker.problems.length > 0) { if (checker.problems.length > 0) {
throw new Error(`Schema does not specify a bijection:\n` + return { ok: false, problems: checker.problems };
checker.problems.map(c => ' - ' + c).join('\n')); } else {
return { ok: true, schema };
} }
return schema;
} }
class Checker { class Checker {

View File

@ -59,7 +59,10 @@ function _readSchema(source: string, options?: ReaderOptions<never>): Array<Inpu
export function readSchema(source: string, export function readSchema(source: string,
options?: ReaderOptions<never> & SchemaReaderOptions): Schema options?: ReaderOptions<never> & SchemaReaderOptions): Schema
{ {
return checkSchema(parseSchema(_readSchema(source, options), options ?? {})); const checked = checkSchema(parseSchema(_readSchema(source, options), options ?? {}));
if (checked.ok) return checked.schema;
throw new Error(`Schema does not specify a bijection:\n` +
checked.problems.map(c => ' - ' + c).join('\n'));
} }
export function parseSchema(toplevelTokens: Array<Input>, export function parseSchema(toplevelTokens: Array<Input>,