Skip to content

Commit

Permalink
[schemer] bump AJV, bump other deps (expo#3423)
Browse files Browse the repository at this point in the history
* [schemer] bum AJV, bump other deps

* restore rootDir (probably breaking change)

* fix rootDir for test

* schemer options refactor, better backward compatibility

* fixes and regeneration after sync
  • Loading branch information
Simek committed Oct 3, 2022
1 parent c4da68c commit 97e299f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 43 deletions.
12 changes: 6 additions & 6 deletions packages/schemer/__tests__/__snapshots__/test.js.snap
Expand Up @@ -12,9 +12,9 @@ Array [
"sdkVersion": "17.0.0abad",
"slug": "1*@)#($*@)(#$*)",
},
"errorCode": "SCHEMA_ADDITIONAL_PROPERTY",
"errorCode": "SCHEMA_MISSING_REQUIRED_PROPERTY",
"fieldPath": "",
"message": "should NOT have additional property 'orientaasdfasdftion'",
"message": "is missing required property 'name'",
"meta": undefined,
"name": "ValidationError",
},
Expand All @@ -30,7 +30,7 @@ Array [
},
"errorCode": "SCHEMA_ADDITIONAL_PROPERTY",
"fieldPath": "",
"message": "should NOT have additional property 'asdfasdfandroid'",
"message": "should NOT have additional property 'orientaasdfasdftion'",
"meta": undefined,
"name": "ValidationError",
},
Expand All @@ -44,17 +44,17 @@ Array [
"sdkVersion": "17.0.0abad",
"slug": "1*@)#($*@)(#$*)",
},
"errorCode": "SCHEMA_MISSING_REQUIRED_PROPERTY",
"errorCode": "SCHEMA_ADDITIONAL_PROPERTY",
"fieldPath": "",
"message": "is missing required property 'name'",
"message": "should NOT have additional property 'asdfasdfandroid'",
"meta": undefined,
"name": "ValidationError",
},
Object {
"data": "1*@)#($*@)(#$*)",
"errorCode": "SCHEMA_INVALID_PATTERN",
"fieldPath": "slug",
"message": "'slug' should match pattern \\"^[a-zA-Z0-9_\\\\-]+$\\"",
"message": "'slug' must match pattern \\"^[a-zA-Z0-9_\\\\-]+$\\"",
"meta": undefined,
"name": "ValidationError",
},
Expand Down
9 changes: 5 additions & 4 deletions packages/schemer/package.json
Expand Up @@ -27,10 +27,11 @@
"@types/lodash": "^4.14.176"
},
"dependencies": {
"ajv": "^6.12.6",
"json-schema-traverse": "0.3.1",
"lodash": "^4.17.19",
"probe-image-size": "~6.0.0",
"ajv": "^8.1.0",
"ajv-formats": "^2.0.2",
"json-schema-traverse": "^1.0.0",
"lodash": "^4.17.21",
"probe-image-size": "^7.1.0",
"read-chunk": "^3.2.0"
},
"publishConfig": {
Expand Down
51 changes: 25 additions & 26 deletions packages/schemer/src/index.ts
@@ -1,4 +1,5 @@
import Ajv from 'ajv';
import Ajv, { ErrorObject, Options } from 'ajv';
import addFormats from 'ajv-formats';
import fs from 'fs';
import traverse from 'json-schema-traverse';
import get from 'lodash/get';
Expand All @@ -9,14 +10,6 @@ import readChunk from 'read-chunk';
import { SchemerError, ValidationError } from './Error';
import { fieldPathToSchema, schemaPointerToFieldPath } from './Util';

type Options = {
allErrors?: boolean;
rootDir?: string;
verbose?: boolean;
format?: 'full' | 'empty'; //figure out later
metaValidation?: boolean;
};

type Meta = {
asset?: boolean;
dimensions?: {
Expand All @@ -28,47 +21,53 @@ type Meta = {
contentTypeHuman?: string;
};

type SchemerOptions = Options & {
rootDir?: string;
};

type AssetField = { fieldPath: string; data: string; meta: Meta };

export { SchemerError, ValidationError, ErrorCodes, ErrorCode } from './Error';
export default class Schemer {
options: Options;
ajv: Ajv.Ajv;
options: SchemerOptions;
ajv: Ajv;
schema: object;
rootDir: string;
manualValidationErrors: ValidationError[];
// Schema is a JSON Schema object
constructor(schema: object, options: Options = {}) {
constructor(schema: object, options: SchemerOptions = {}) {
this.options = {
allErrors: true,
verbose: true,
format: 'full',
metaValidation: true,
meta: true,
strict: false,
unicodeRegExp: false,
...options,
};

this.ajv = new Ajv(this.options);
addFormats(this.ajv, { mode: 'full' });
this.schema = schema;
this.rootDir = this.options.rootDir || __dirname;
this.manualValidationErrors = [];
}

_formatAjvErrorMessage({
keyword,
dataPath,
instancePath,
params,
parentSchema,
data,
message,
}: Ajv.ErrorObject) {
}: ErrorObject) {
const meta = parentSchema && (parentSchema as any).meta;
// This removes the "." in front of a fieldPath
dataPath = dataPath.slice(1);
instancePath = instancePath.slice(1);
switch (keyword) {
case 'additionalProperties': {
return new ValidationError({
errorCode: 'SCHEMA_ADDITIONAL_PROPERTY',
fieldPath: dataPath,
fieldPath: instancePath,
message: `should NOT have additional property '${(params as any).additionalProperty}'`,
data,
meta,
Expand All @@ -77,7 +76,7 @@ export default class Schemer {
case 'required':
return new ValidationError({
errorCode: 'SCHEMA_MISSING_REQUIRED_PROPERTY',
fieldPath: dataPath,
fieldPath: instancePath,
message: `is missing required property '${(params as any).missingProperty}'`,
data,
meta,
Expand All @@ -86,11 +85,11 @@ export default class Schemer {
//@TODO Parse the message in a less hacky way. Perhaps for regex validation errors, embed the error message under the meta tag?
const regexHuman = meta?.regexHuman;
const regexErrorMessage = regexHuman
? `'${dataPath}' should be a ${regexHuman[0].toLowerCase() + regexHuman.slice(1)}`
: `'${dataPath}' ${message}`;
? `'${instancePath}' should be a ${regexHuman[0].toLowerCase() + regexHuman.slice(1)}`
: `'${instancePath}' ${message}`;
return new ValidationError({
errorCode: 'SCHEMA_INVALID_PATTERN',
fieldPath: dataPath,
fieldPath: instancePath,
message: regexErrorMessage,
data,
meta,
Expand All @@ -99,11 +98,11 @@ export default class Schemer {
case 'not': {
const notHuman = meta?.notHuman;
const notHumanErrorMessage = notHuman
? `'${dataPath}' should be ${notHuman[0].toLowerCase() + notHuman.slice(1)}`
: `'${dataPath}' ${message}`;
? `'${instancePath}' should be ${notHuman[0].toLowerCase() + notHuman.slice(1)}`
: `'${instancePath}' ${message}`;
return new ValidationError({
errorCode: 'SCHEMA_INVALID_NOT',
fieldPath: dataPath,
fieldPath: instancePath,
message: notHumanErrorMessage,
data,
meta,
Expand All @@ -112,7 +111,7 @@ export default class Schemer {
default:
return new ValidationError({
errorCode: 'SCHEMA_VALIDATION_ERROR',
fieldPath: dataPath,
fieldPath: instancePath,
message: message || 'Validation error',
data,
meta,
Expand Down
40 changes: 33 additions & 7 deletions yarn.lock
Expand Up @@ -4475,6 +4475,13 @@ ajv-errors@^1.0.0:
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d"
integrity sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==

ajv-formats@^2.0.2:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ajv-formats/-/ajv-formats-2.1.1.tgz#6e669400659eb74973bbf2e33327180a0996b520"
integrity sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==
dependencies:
ajv "^8.0.0"

ajv-keywords@^3.1.0, ajv-keywords@^3.4.1, ajv-keywords@^3.5.2:
version "3.5.2"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-3.5.2.tgz#31f29da5ab6e00d1c2d329acf7b5929614d5014d"
Expand All @@ -4490,7 +4497,7 @@ ajv@6.5.3:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4, ajv@^6.12.5, ajv@^6.12.6, ajv@^6.5.5:
ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4, ajv@^6.12.5, ajv@^6.5.5:
version "6.12.6"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
integrity sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==
Expand All @@ -4500,6 +4507,16 @@ ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4, ajv@^6.12.5, ajv@^6.12.6, ajv
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^8.0.0, ajv@^8.1.0:
version "8.11.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.11.0.tgz#977e91dd96ca669f54a11e23e378e33b884a565f"
integrity sha512-wGgprdCvMalC0BztXvitD2hC04YffAvtsUn93JbGXYLAtCUO4xd17mCCZQxUOItiBwZvJScWo8NIvQMQ71rdpg==
dependencies:
fast-deep-equal "^3.1.1"
json-schema-traverse "^1.0.0"
require-from-string "^2.0.2"
uri-js "^4.2.2"

ally.js@1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/ally.js/-/ally.js-1.4.1.tgz#9fb7e6ba58efac4ee9131cb29aa9ee3b540bcf1e"
Expand Down Expand Up @@ -11782,16 +11799,16 @@ json-schema-deref-sync@^0.13.0:
traverse "~0.6.6"
valid-url "~1.0.9"

json-schema-traverse@0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
integrity sha1-NJptRMU6Ud6JtAgFxdXlm0F9M0A=

json-schema-traverse@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
integrity sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==

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==

json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
Expand Down Expand Up @@ -12304,7 +12321,7 @@ lodash.uniq@^4.5.0:
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=

"lodash@>=3.5 <5", lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.7.0:
"lodash@>=3.5 <5", lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.2.1, lodash@^4.7.0:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down Expand Up @@ -15072,6 +15089,15 @@ private@^0.1.8:
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"
integrity sha512-VvivMrbvd2nKkiG38qjULzlc+4Vx4wm/whI9pQD35YrARNnhxeiRktSOhSukRLFNlzg6Br/cJPet5J/u19r/mg==

probe-image-size@^7.1.0:
version "7.2.3"
resolved "https://registry.yarnpkg.com/probe-image-size/-/probe-image-size-7.2.3.tgz#d49c64be540ec8edea538f6f585f65a9b3ab4309"
integrity sha512-HubhG4Rb2UH8YtV4ba0Vp5bQ7L78RTONYu/ujmCu5nBI8wGv24s4E9xSKBi0N1MowRpxk76pFCpJtW0KPzOK0w==
dependencies:
lodash.merge "^4.6.2"
needle "^2.5.2"
stream-parser "~0.3.1"

probe-image-size@~6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/probe-image-size/-/probe-image-size-6.0.0.tgz#4a85b19d5af4e29a8de7d53a9aa036f6fd02f5f4"
Expand Down

0 comments on commit 97e299f

Please sign in to comment.