Skip to content

Commit

Permalink
Add JTD validation with Ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
atesgoral committed Aug 11, 2021
1 parent 30cdd99 commit 4ab083b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"homepage": "https://github.com/atesgoral/acb#readme",
"dependencies": {
"ajv": "^8.6.2",
"stream-parser": "^0.3.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pkg from './package.json';

export default defineConfig({
input: 'src/index.ts',
external: ['stream-parser', 'stream'],
external: ['stream-parser', 'ajv/dist/jtd', 'stream'],
plugins: [typescript({tsconfig: './tsconfig.json'})],
output: [
{file: pkg.main, format: 'cjs'},
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function generateBook({
colors: Iterable<Color>;
}) {
const book: ColorBook = {
id: 3090 + id,
id: 40 + id,
colorSpace,
title: `${colorSpace} Components`,
description: 'Example color book to verify component conversion',
Expand Down
9 changes: 9 additions & 0 deletions src/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ColorSpace, ColorBook} from './types';
import validate from './validator';
import * as Chunk from './chunk';

const ColorSpaceToId: Record<ColorSpace, number> = {
Expand All @@ -14,6 +15,14 @@ const ColorSpaceComponents: Record<ColorSpace, number> = {
};

export function* encodeAcb(book: ColorBook) {
const valid = validate(book);

if (!valid) {
throw new Error(
`Validation failed: ${JSON.stringify(validate.errors, null, 2)}`
);
}

yield Chunk.fromAscii('8BCB');
yield Chunk.fromUInt16BE(1);

Expand Down
33 changes: 33 additions & 0 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Ajv, {JTDSchemaType} from 'ajv/dist/jtd';

import {ColorBook} from './types';

const schema: JTDSchemaType<ColorBook> = {
properties: {
id: {type: 'uint16'},
title: {type: 'string'},
colorNamePrefix: {type: 'string'},
colorNameSuffix: {type: 'string'},
description: {type: 'string'},
pageSize: {type: 'uint16'},
pageMidPoint: {type: 'uint16'},
colorSpace: {enum: ['RGB', 'CMYK', 'Lab']},
colors: {
elements: {
properties: {
name: {type: 'string'},
code: {type: 'string'},
components: {
elements: {type: 'int16'},
},
},
},
},
isSpot: {type: 'boolean'},
},
};

const ajv = new Ajv();
const validate = ajv.compile(schema);

export default validate;
34 changes: 33 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ agent-base@6:
dependencies:
debug "4"

ajv@^8.6.2:
version "8.6.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.6.2.tgz#2fb45e0e5fcbc0813326c1c3da535d1881bb0571"
integrity sha512-9807RlWAgT564wT+DjeyU5OFMPjmzxVobvDFmNAhY+5zD6A2ly3jDp6sgnfyDtlIQ+7H97oc/DGCzzfu9rjw9w==
dependencies:
fast-deep-equal "^3.1.1"
json-schema-traverse "^1.0.0"
require-from-string "^2.0.2"
uri-js "^4.2.2"

ansi-escapes@^4.2.1:
version "4.3.2"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"
Expand Down Expand Up @@ -1216,6 +1226,11 @@ expect@^27.0.6:
jest-message-util "^27.0.6"
jest-regex-util "^27.0.6"

fast-deep-equal@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

[email protected], fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down Expand Up @@ -1968,6 +1983,11 @@ jsesc@^2.5.1:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==

json-schema-traverse@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==

[email protected], json5@^2.1.2:
version "2.2.0"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3"
Expand Down Expand Up @@ -2262,7 +2282,7 @@ psl@^1.1.33:
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
integrity sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==

punycode@^2.1.1:
punycode@^2.1.0, punycode@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
Expand All @@ -2277,6 +2297,11 @@ require-directory@^2.1.1:
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==

resolve-cwd@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/resolve-cwd/-/resolve-cwd-3.0.0.tgz#0f0075f1bb2544766cf73ba6a6e2adfebcb13f2d"
Expand Down Expand Up @@ -2605,6 +2630,13 @@ universalify@^0.1.2:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

uri-js@^4.2.2:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"
integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==
dependencies:
punycode "^2.1.0"

v8-to-istanbul@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-8.0.0.tgz#4229f2a99e367f3f018fa1d5c2b8ec684667c69c"
Expand Down

0 comments on commit 4ab083b

Please sign in to comment.