From 5dcd61634fcc672da87e618f647576ac9e161002 Mon Sep 17 00:00:00 2001 From: Jakob Voss Date: Sun, 14 Jan 2024 17:48:26 +0100 Subject: [PATCH] Support setting record type(s) --- CHANGELOG.md | 6 ++++++ bin/avram.js | 1 + lib/action.js | 8 ++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c84fe85..317cf30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. +## 0.6.1 - 2024-01-14 + +- Support setting record type(s) +- Extend error messages +- Fix Avram version number + ## 0.6.0 - 2024-01-13 - Update metaschema and validation rules to Avram 0.9.5 diff --git a/bin/avram.js b/bin/avram.js index 228ed65..d2f99e8 100755 --- a/bin/avram.js +++ b/bin/avram.js @@ -7,6 +7,7 @@ cli.usage("avram [options] [validation options] []") .description("Validate file(s) with an Avram schema") .option(`-f, --format [name] input format (${Object.keys(formats).join("|")})`) .option("-s, --schema validate schema instead of record files") + .option("-t, --type [types] specify comma-separated record type(s)") .option("-p, --print print all input records (in JSON)") .option("-v, --verbose verbose error messages") .option("-l, --list list supported validation options") diff --git a/lib/action.js b/lib/action.js index 6cd250c..59e6d55 100644 --- a/lib/action.js +++ b/lib/action.js @@ -61,7 +61,10 @@ export default async (args, opt) => { throw new Error(`Unknown or unsupported format '${opt.format}'`) } - const schemaFile = opt.schema && !files.length ? "/dev/stdin" : files.shift() + const types = typeof opt.type == "string" ? opt.type.split(",") : [] + + const schemaFile = (opt.schema && !files.length) ? "/dev/stdin" : files.shift() + if (schemaFile === undefined) throw new Error("Missing schema argument") const schema = loadSchema(schemaFile, opt) const validator = new Validator(schema, options) @@ -83,7 +86,8 @@ export default async (args, opt) => { const input = file === "-" ? process.stdin : fs.createReadStream(file) const stream = format.stream(input) // TODO: marcxml parser should emit error on parsing error - stream.on("data", record => { + stream.on("data", fields => { + const record = { fields, types } const errors = validator.validate(record) errors.forEach(e => { if (file !== "-") {