Created
December 30, 2021 14:42
-
-
Save mitchallen/e492983c1298fdc53a5443714733e964 to your computer and use it in GitHub Desktop.
A less readable but more efficient parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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