Skip to content

Instantly share code, notes, and snippets.

@mitchallen
Created December 30, 2021 14:42
Show Gist options
  • Select an option

  • Save mitchallen/e492983c1298fdc53a5443714733e964 to your computer and use it in GitHub Desktop.

Select an option

Save mitchallen/e492983c1298fdc53a5443714733e964 to your computer and use it in GitHub Desktop.
A less readable but more efficient parser
// Author: Mitch Allen
// File: parser.js
import { readFileSync } from 'fs';
import Ajv from 'ajv';
const ajv = new Ajv();
export function parser(inputFile, schemaFile) {
let [input, schema] = [
inputFile,
schemaFile
].map(file => JSON.parse(readFileSync(file)));
const isValid = ajv.validate(schema, input);
if (!isValid) {
console.error(JSON.stringify(ajv.errors, null, 2));
return undefined;
}
console.info('[INFO] Valid!');
return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment