@expo/schemer 1.4.3 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +8 -12
- package/build/index.js +16 -13
- package/build/index.js.map +1 -1
- package/package.json +6 -5
package/build/index.d.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import Ajv from 'ajv';
|
|
1
|
+
import Ajv, { ErrorObject, Options } from 'ajv';
|
|
2
2
|
import { ValidationError } from './Error';
|
|
3
|
-
declare type Options = {
|
|
4
|
-
allErrors?: boolean;
|
|
5
|
-
rootDir?: string;
|
|
6
|
-
verbose?: boolean;
|
|
7
|
-
format?: 'full' | 'empty';
|
|
8
|
-
metaValidation?: boolean;
|
|
9
|
-
};
|
|
10
3
|
declare type Meta = {
|
|
11
4
|
asset?: boolean;
|
|
12
5
|
dimensions?: {
|
|
@@ -17,6 +10,9 @@ declare type Meta = {
|
|
|
17
10
|
contentTypePattern?: string;
|
|
18
11
|
contentTypeHuman?: string;
|
|
19
12
|
};
|
|
13
|
+
declare type SchemerOptions = Options & {
|
|
14
|
+
rootDir?: string;
|
|
15
|
+
};
|
|
20
16
|
declare type AssetField = {
|
|
21
17
|
fieldPath: string;
|
|
22
18
|
data: string;
|
|
@@ -24,13 +20,13 @@ declare type AssetField = {
|
|
|
24
20
|
};
|
|
25
21
|
export { SchemerError, ValidationError, ErrorCodes, ErrorCode } from './Error';
|
|
26
22
|
export default class Schemer {
|
|
27
|
-
options:
|
|
28
|
-
ajv: Ajv
|
|
23
|
+
options: SchemerOptions;
|
|
24
|
+
ajv: Ajv;
|
|
29
25
|
schema: object;
|
|
30
26
|
rootDir: string;
|
|
31
27
|
manualValidationErrors: ValidationError[];
|
|
32
|
-
constructor(schema: object, options?:
|
|
33
|
-
_formatAjvErrorMessage({ keyword,
|
|
28
|
+
constructor(schema: object, options?: SchemerOptions);
|
|
29
|
+
_formatAjvErrorMessage({ keyword, instancePath, params, parentSchema, data, message, }: ErrorObject): ValidationError;
|
|
34
30
|
getErrors(): ValidationError[];
|
|
35
31
|
_throwOnErrors(): void;
|
|
36
32
|
validateAll(data: any): Promise<void>;
|
package/build/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ErrorCodes = exports.ValidationError = exports.SchemerError = void 0;
|
|
7
7
|
const ajv_1 = __importDefault(require("ajv"));
|
|
8
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
8
9
|
const fs_1 = __importDefault(require("fs"));
|
|
9
10
|
const json_schema_traverse_1 = __importDefault(require("json-schema-traverse"));
|
|
10
11
|
const get_1 = __importDefault(require("lodash/get"));
|
|
@@ -23,24 +24,26 @@ class Schemer {
|
|
|
23
24
|
this.options = {
|
|
24
25
|
allErrors: true,
|
|
25
26
|
verbose: true,
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
meta: true,
|
|
28
|
+
strict: false,
|
|
29
|
+
unicodeRegExp: false,
|
|
28
30
|
...options,
|
|
29
31
|
};
|
|
30
32
|
this.ajv = new ajv_1.default(this.options);
|
|
33
|
+
(0, ajv_formats_1.default)(this.ajv, { mode: 'full' });
|
|
31
34
|
this.schema = schema;
|
|
32
35
|
this.rootDir = this.options.rootDir || __dirname;
|
|
33
36
|
this.manualValidationErrors = [];
|
|
34
37
|
}
|
|
35
|
-
_formatAjvErrorMessage({ keyword,
|
|
38
|
+
_formatAjvErrorMessage({ keyword, instancePath, params, parentSchema, data, message, }) {
|
|
36
39
|
const meta = parentSchema && parentSchema.meta;
|
|
37
40
|
// This removes the "." in front of a fieldPath
|
|
38
|
-
|
|
41
|
+
instancePath = instancePath.slice(1);
|
|
39
42
|
switch (keyword) {
|
|
40
43
|
case 'additionalProperties': {
|
|
41
44
|
return new Error_1.ValidationError({
|
|
42
45
|
errorCode: 'SCHEMA_ADDITIONAL_PROPERTY',
|
|
43
|
-
fieldPath:
|
|
46
|
+
fieldPath: instancePath,
|
|
44
47
|
message: `should NOT have additional property '${params.additionalProperty}'`,
|
|
45
48
|
data,
|
|
46
49
|
meta,
|
|
@@ -49,7 +52,7 @@ class Schemer {
|
|
|
49
52
|
case 'required':
|
|
50
53
|
return new Error_1.ValidationError({
|
|
51
54
|
errorCode: 'SCHEMA_MISSING_REQUIRED_PROPERTY',
|
|
52
|
-
fieldPath:
|
|
55
|
+
fieldPath: instancePath,
|
|
53
56
|
message: `is missing required property '${params.missingProperty}'`,
|
|
54
57
|
data,
|
|
55
58
|
meta,
|
|
@@ -58,11 +61,11 @@ class Schemer {
|
|
|
58
61
|
//@TODO Parse the message in a less hacky way. Perhaps for regex validation errors, embed the error message under the meta tag?
|
|
59
62
|
const regexHuman = meta === null || meta === void 0 ? void 0 : meta.regexHuman;
|
|
60
63
|
const regexErrorMessage = regexHuman
|
|
61
|
-
? `'${
|
|
62
|
-
: `'${
|
|
64
|
+
? `'${instancePath}' should be a ${regexHuman[0].toLowerCase() + regexHuman.slice(1)}`
|
|
65
|
+
: `'${instancePath}' ${message}`;
|
|
63
66
|
return new Error_1.ValidationError({
|
|
64
67
|
errorCode: 'SCHEMA_INVALID_PATTERN',
|
|
65
|
-
fieldPath:
|
|
68
|
+
fieldPath: instancePath,
|
|
66
69
|
message: regexErrorMessage,
|
|
67
70
|
data,
|
|
68
71
|
meta,
|
|
@@ -71,11 +74,11 @@ class Schemer {
|
|
|
71
74
|
case 'not': {
|
|
72
75
|
const notHuman = meta === null || meta === void 0 ? void 0 : meta.notHuman;
|
|
73
76
|
const notHumanErrorMessage = notHuman
|
|
74
|
-
? `'${
|
|
75
|
-
: `'${
|
|
77
|
+
? `'${instancePath}' should be ${notHuman[0].toLowerCase() + notHuman.slice(1)}`
|
|
78
|
+
: `'${instancePath}' ${message}`;
|
|
76
79
|
return new Error_1.ValidationError({
|
|
77
80
|
errorCode: 'SCHEMA_INVALID_NOT',
|
|
78
|
-
fieldPath:
|
|
81
|
+
fieldPath: instancePath,
|
|
79
82
|
message: notHumanErrorMessage,
|
|
80
83
|
data,
|
|
81
84
|
meta,
|
|
@@ -84,7 +87,7 @@ class Schemer {
|
|
|
84
87
|
default:
|
|
85
88
|
return new Error_1.ValidationError({
|
|
86
89
|
errorCode: 'SCHEMA_VALIDATION_ERROR',
|
|
87
|
-
fieldPath:
|
|
90
|
+
fieldPath: instancePath,
|
|
88
91
|
message: message || 'Validation error',
|
|
89
92
|
data,
|
|
90
93
|
meta,
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAsB;AACtB,4CAAoB;AACpB,gFAA4C;AAC5C,qDAA6B;AAC7B,gDAAwB;AACxB,wEAA0C;AAC1C,4DAAmC;AAEnC,mCAAwD;AACxD,iCAAqE;AAuBrE,iCAA+E;AAAtE,qGAAA,YAAY,OAAA;AAAE,wGAAA,eAAe,OAAA;AAAE,mGAAA,UAAU,OAAA;AAClD,MAAqB,OAAO;IAM1B,iCAAiC;IACjC,YAAY,MAAc,EAAE,UAAmB,EAAE;QAC/C,IAAI,CAAC,OAAO,GAAG;YACb,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,IAAI;YACb,MAAM,EAAE,MAAM;YACd,cAAc,EAAE,IAAI;YACpB,GAAG,OAAO;SACX,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,IAAI,aAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;QACjD,IAAI,CAAC,sBAAsB,GAAG,EAAE,CAAC;IACnC,CAAC;IAED,sBAAsB,CAAC,EACrB,OAAO,EACP,QAAQ,EACR,MAAM,EACN,YAAY,EACZ,IAAI,EACJ,O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AS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;aAC3D;SACF;IACH,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,SAAiB,EAAE,IAAS;QACjD,MAAM,SAAS,GAAG,IAAA,wBAAiB,EAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAC5D,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE;YAC1C,MAAM,IAAI,CAAC,mBAAmB,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;SAC3E;QACD,IAAI,CAAC,cAAc,EAAE,CAAC;IACxB,CAAC;IAED,YAAY,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,YAAY,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,kBAAkB,CAAC,OAAe;QAChC,OAAO,IAAI,CAAC,gBAAgB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,YAAY,CAAC,QAAgB;QAC3B,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;CACF;AAtPD,0BAsPC","sourcesContent":["import Ajv from 'ajv';\nimport fs from 'fs';\nimport traverse from 'json-schema-traverse';\nimport get from 'lodash/get';\nimport path from 'path';\nimport imageProbe from 'probe-image-size';\nimport readChunk from 'read-chunk';\n\nimport { SchemerError, ValidationError } from './Error';\nimport { fieldPathToSchema, schemaPointerToFieldPath } from './Util';\n\ntype Options = {\n allErrors?: boolean;\n rootDir?: string;\n verbose?: boolean;\n format?: 'full' | 'empty'; //figure out later\n metaValidation?: boolean;\n};\n\ntype Meta = {\n asset?: boolean;\n dimensions?: {\n width: number;\n height: number;\n };\n square?: boolean;\n contentTypePattern?: string;\n contentTypeHuman?: string;\n};\n\ntype AssetField = { fieldPath: string; data: string; meta: Meta };\n\nexport { SchemerError, ValidationError, ErrorCodes, ErrorCode } from './Error';\nexport default class Schemer {\n options: Options;\n ajv: Ajv.Ajv;\n schema: object;\n rootDir: string;\n manualValidationErrors: ValidationError[];\n // Schema is a JSON Schema object\n constructor(schema: object, options: Options = {}) {\n this.options = {\n allErrors: true,\n verbose: true,\n format: 'full',\n metaValidation: true,\n ...options,\n };\n\n this.ajv = new Ajv(this.options);\n this.schema = schema;\n this.rootDir = this.options.rootDir || __dirname;\n this.manualValidationErrors = [];\n }\n\n _formatAjvErrorMessage({\n keyword,\n dataPath,\n params,\n parentSchema,\n data,\n message,\n }: Ajv.ErrorObject) {\n const meta = parentSchema && (parentSchema as any).meta;\n // This removes the \".\" in front of a fieldPath\n dataPath = dataPath.slice(1);\n switch (keyword) {\n case 'additionalProperties': {\n return new ValidationError({\n errorCode: 'SCHEMA_ADDITIONAL_PROPERTY',\n fieldPath: dataPath,\n message: `should NOT have additional property '${(params as any).additionalProperty}'`,\n data,\n meta,\n });\n }\n case 'required':\n return new ValidationError({\n errorCode: 'SCHEMA_MISSING_REQUIRED_PROPERTY',\n fieldPath: dataPath,\n message: `is missing required property '${(params as any).missingProperty}'`,\n data,\n meta,\n });\n case 'pattern': {\n //@TODO Parse the message in a less hacky way. Perhaps for regex validation errors, embed the error message under the meta tag?\n const regexHuman = meta?.regexHuman;\n const regexErrorMessage = regexHuman\n ? `'${dataPath}' should be a ${regexHuman[0].toLowerCase() + regexHuman.slice(1)}`\n : `'${dataPath}' ${message}`;\n return new ValidationError({\n errorCode: 'SCHEMA_INVALID_PATTERN',\n fieldPath: dataPath,\n message: regexErrorMessage,\n data,\n meta,\n });\n }\n case 'not': {\n const notHuman = meta?.notHuman;\n const notHumanErrorMessage = notHuman\n ? `'${dataPath}' should be ${notHuman[0].toLowerCase() + notHuman.slice(1)}`\n : `'${dataPath}' ${message}`;\n return new ValidationError({\n errorCode: 'SCHEMA_INVALID_NOT',\n fieldPath: dataPath,\n message: notHumanErrorMessage,\n data,\n meta,\n });\n }\n default:\n return new ValidationError({\n errorCode: 'SCHEMA_VALIDATION_ERROR',\n fieldPath: dataPath,\n message: message || 'Validation error',\n data,\n meta,\n });\n }\n }\n\n getErrors(): ValidationError[] {\n // Convert AJV JSONSchema errors to our ValidationErrors\n let valErrors: ValidationError[] = [];\n if (this.ajv.errors) {\n valErrors = this.ajv.errors.map(e => this._formatAjvErrorMessage(e));\n }\n return [...valErrors, ...this.manualValidationErrors];\n }\n\n _throwOnErrors() {\n // Clean error state after each validation\n const errors = this.getErrors();\n if (errors.length > 0) {\n this.manualValidationErrors = [];\n this.ajv.errors = [];\n throw new SchemerError(errors);\n }\n }\n\n async validateAll(data: any) {\n await this._validateSchemaAsync(data);\n await this._validateAssetsAsync(data);\n this._throwOnErrors();\n }\n\n async validateAssetsAsync(data: any) {\n await this._validateAssetsAsync(data);\n this._throwOnErrors();\n }\n\n async validateSchemaAsync(data: any) {\n await this._validateSchemaAsync(data);\n this._throwOnErrors();\n }\n\n _validateSchemaAsync(data: any) {\n this.ajv.validate(this.schema, data);\n }\n\n async _validateAssetsAsync(data: any) {\n const assets: AssetField[] = [];\n traverse(this.schema, { allKeys: true }, (subSchema, jsonPointer, a, b, c, d, property) => {\n if (property && subSchema.meta && subSchema.meta.asset) {\n const fieldPath = schemaPointerToFieldPath(jsonPointer);\n assets.push({\n fieldPath,\n data: get(data, fieldPath),\n meta: subSchema.meta,\n });\n }\n });\n await Promise.all(assets.map(this._validateAssetAsync.bind(this)));\n }\n\n async _validateImageAsync({ fieldPath, data, meta }: AssetField) {\n if (meta && meta.asset && data) {\n const { dimensions, square, contentTypePattern }: Meta = meta;\n // filePath could be an URL\n const filePath = path.resolve(this.rootDir, data);\n try {\n // NOTE(nikki): The '4100' below should be enough for most file types, though we\n // could probably go shorter....\n // http://www.garykessler.net/library/file_sigs.html\n // The metadata content for .jpgs might be located a lot farther down the file, so this\n // may pose problems in the future.\n // This cases on whether filePath is a remote URL or located on the machine\n const probeResult = fs.existsSync(filePath)\n ? imageProbe.sync(await readChunk(filePath, 0, 4100))\n : await imageProbe(data, { useElectronNet: false });\n if (!probeResult) {\n return;\n }\n\n const { width, height, type, mime } = probeResult;\n\n if (contentTypePattern && !mime.match(new RegExp(contentTypePattern))) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_CONTENT_TYPE',\n fieldPath,\n message: `field '${fieldPath}' should point to ${meta.contentTypeHuman} but the file at '${data}' has type ${type}`,\n data,\n meta,\n })\n );\n }\n\n if (dimensions && (dimensions.height !== height || dimensions.width !== width)) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_DIMENSIONS',\n fieldPath,\n message: `'${fieldPath}' should have dimensions ${dimensions.width}x${dimensions.height}, but the file at '${data}' has dimensions ${width}x${height}`,\n data,\n meta,\n })\n );\n }\n\n if (square && width !== height) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'NOT_SQUARE',\n fieldPath,\n message: `image should be square, but the file at '${data}' has dimensions ${width}x${height}`,\n data,\n meta,\n })\n );\n }\n } catch {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_ASSET_URI',\n fieldPath,\n message: `cannot access file at '${data}'`,\n data,\n meta,\n })\n );\n }\n }\n }\n\n async _validateAssetAsync({ fieldPath, data, meta }: AssetField) {\n if (meta && meta.asset && data) {\n if (meta.contentTypePattern && meta.contentTypePattern.startsWith('^image')) {\n await this._validateImageAsync({ fieldPath, data, meta });\n }\n }\n }\n\n async validateProperty(fieldPath: string, data: any) {\n const subSchema = fieldPathToSchema(this.schema, fieldPath);\n this.ajv.validate(subSchema, data);\n\n if (subSchema.meta && subSchema.meta.asset) {\n await this._validateAssetAsync({ fieldPath, data, meta: subSchema.meta });\n }\n this._throwOnErrors();\n }\n\n validateName(name: string) {\n return this.validateProperty('name', name);\n }\n\n validateSlug(slug: string) {\n return this.validateProperty('slug', slug);\n }\n\n validateSdkVersion(version: string) {\n return this.validateProperty('sdkVersion', version);\n }\n\n validateIcon(iconPath: string) {\n return this.validateProperty('icon', iconPath);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAgD;AAChD,8DAAqC;AACrC,4CAAoB;AACpB,gFAA4C;AAC5C,qDAA6B;AAC7B,gDAAwB;AACxB,wEAA0C;AAC1C,4DAAmC;AAEnC,mCAAwD;AACxD,iCAAqE;AAmBrE,iCAA+E;AAAtE,qGAAA,YAAY,OAAA;AAAE,wGAAA,eAAe,OAAA;AAAE,mGAAA,UAAU,OAAA;AAClD,MAAqB,OAAO;IAM1B,iCAAiC;IACjC,YAAY,MAAc,EAAE,UAA0B,EAAE;QACtD,IAAI,CAAC,OAAO,GAAG;YACb,SAAS,EAAE,IAAI;YACf,OAAO,EAAE,IAAI;YACb,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,KAAK;YACb,aAAa,EAAE,KAAK;YACpB,GAAG,OAAO;SACX,CAAC;QAEF,IAAI,CAAC,GAAG,GAAG,IAAI,aAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACjC,IAAA,qBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,SAAS,CAAC;QACjD,IAAI,CAAC,sBAAsB,GAAG,EAAE,CAAC;IACnC,CAAC;IAED,sBAAsB,CAAC,EACrB,OAAO,EACP,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,IAAI,EACJ,OAAO,GACK;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EAAE,CAAC,CAAC;aAC3D;SACF;IACH,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,SAAiB,EAAE,IAAS;QACjD,MAAM,SAAS,GAAG,IAAA,wBAAiB,EAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;QAC5D,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE;YAC1C,MAAM,IAAI,CAAC,mBAAmB,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;SAC3E;QACD,IAAI,CAAC,cAAc,EAAE,CAAC;IACxB,CAAC;IAED,YAAY,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,YAAY,CAAC,IAAY;QACvB,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAC7C,CAAC;IAED,kBAAkB,CAAC,OAAe;QAChC,OAAO,IAAI,CAAC,gBAAgB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACtD,CAAC;IAED,YAAY,CAAC,QAAgB;QAC3B,OAAO,IAAI,CAAC,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;CACF;AAxPD,0BAwPC","sourcesContent":["import Ajv, { ErrorObject, Options } from 'ajv';\nimport addFormats from 'ajv-formats';\nimport fs from 'fs';\nimport traverse from 'json-schema-traverse';\nimport get from 'lodash/get';\nimport path from 'path';\nimport imageProbe from 'probe-image-size';\nimport readChunk from 'read-chunk';\n\nimport { SchemerError, ValidationError } from './Error';\nimport { fieldPathToSchema, schemaPointerToFieldPath } from './Util';\n\ntype Meta = {\n asset?: boolean;\n dimensions?: {\n width: number;\n height: number;\n };\n square?: boolean;\n contentTypePattern?: string;\n contentTypeHuman?: string;\n};\n\ntype SchemerOptions = Options & {\n rootDir?: string;\n};\n\ntype AssetField = { fieldPath: string; data: string; meta: Meta };\n\nexport { SchemerError, ValidationError, ErrorCodes, ErrorCode } from './Error';\nexport default class Schemer {\n options: SchemerOptions;\n ajv: Ajv;\n schema: object;\n rootDir: string;\n manualValidationErrors: ValidationError[];\n // Schema is a JSON Schema object\n constructor(schema: object, options: SchemerOptions = {}) {\n this.options = {\n allErrors: true,\n verbose: true,\n meta: true,\n strict: false,\n unicodeRegExp: false,\n ...options,\n };\n\n this.ajv = new Ajv(this.options);\n addFormats(this.ajv, { mode: 'full' });\n this.schema = schema;\n this.rootDir = this.options.rootDir || __dirname;\n this.manualValidationErrors = [];\n }\n\n _formatAjvErrorMessage({\n keyword,\n instancePath,\n params,\n parentSchema,\n data,\n message,\n }: ErrorObject) {\n const meta = parentSchema && (parentSchema as any).meta;\n // This removes the \".\" in front of a fieldPath\n instancePath = instancePath.slice(1);\n switch (keyword) {\n case 'additionalProperties': {\n return new ValidationError({\n errorCode: 'SCHEMA_ADDITIONAL_PROPERTY',\n fieldPath: instancePath,\n message: `should NOT have additional property '${(params as any).additionalProperty}'`,\n data,\n meta,\n });\n }\n case 'required':\n return new ValidationError({\n errorCode: 'SCHEMA_MISSING_REQUIRED_PROPERTY',\n fieldPath: instancePath,\n message: `is missing required property '${(params as any).missingProperty}'`,\n data,\n meta,\n });\n case 'pattern': {\n //@TODO Parse the message in a less hacky way. Perhaps for regex validation errors, embed the error message under the meta tag?\n const regexHuman = meta?.regexHuman;\n const regexErrorMessage = regexHuman\n ? `'${instancePath}' should be a ${regexHuman[0].toLowerCase() + regexHuman.slice(1)}`\n : `'${instancePath}' ${message}`;\n return new ValidationError({\n errorCode: 'SCHEMA_INVALID_PATTERN',\n fieldPath: instancePath,\n message: regexErrorMessage,\n data,\n meta,\n });\n }\n case 'not': {\n const notHuman = meta?.notHuman;\n const notHumanErrorMessage = notHuman\n ? `'${instancePath}' should be ${notHuman[0].toLowerCase() + notHuman.slice(1)}`\n : `'${instancePath}' ${message}`;\n return new ValidationError({\n errorCode: 'SCHEMA_INVALID_NOT',\n fieldPath: instancePath,\n message: notHumanErrorMessage,\n data,\n meta,\n });\n }\n default:\n return new ValidationError({\n errorCode: 'SCHEMA_VALIDATION_ERROR',\n fieldPath: instancePath,\n message: message || 'Validation error',\n data,\n meta,\n });\n }\n }\n\n getErrors(): ValidationError[] {\n // Convert AJV JSONSchema errors to our ValidationErrors\n let valErrors: ValidationError[] = [];\n if (this.ajv.errors) {\n valErrors = this.ajv.errors.map(e => this._formatAjvErrorMessage(e));\n }\n return [...valErrors, ...this.manualValidationErrors];\n }\n\n _throwOnErrors() {\n // Clean error state after each validation\n const errors = this.getErrors();\n if (errors.length > 0) {\n this.manualValidationErrors = [];\n this.ajv.errors = [];\n throw new SchemerError(errors);\n }\n }\n\n async validateAll(data: any) {\n await this._validateSchemaAsync(data);\n await this._validateAssetsAsync(data);\n this._throwOnErrors();\n }\n\n async validateAssetsAsync(data: any) {\n await this._validateAssetsAsync(data);\n this._throwOnErrors();\n }\n\n async validateSchemaAsync(data: any) {\n await this._validateSchemaAsync(data);\n this._throwOnErrors();\n }\n\n _validateSchemaAsync(data: any) {\n this.ajv.validate(this.schema, data);\n }\n\n async _validateAssetsAsync(data: any) {\n const assets: AssetField[] = [];\n traverse(this.schema, { allKeys: true }, (subSchema, jsonPointer, a, b, c, d, property) => {\n if (property && subSchema.meta && subSchema.meta.asset) {\n const fieldPath = schemaPointerToFieldPath(jsonPointer);\n assets.push({\n fieldPath,\n data: get(data, fieldPath),\n meta: subSchema.meta,\n });\n }\n });\n await Promise.all(assets.map(this._validateAssetAsync.bind(this)));\n }\n\n async _validateImageAsync({ fieldPath, data, meta }: AssetField) {\n if (meta && meta.asset && data) {\n const { dimensions, square, contentTypePattern }: Meta = meta;\n // filePath could be an URL\n const filePath = path.resolve(this.rootDir, data);\n try {\n // NOTE(nikki): The '4100' below should be enough for most file types, though we\n // could probably go shorter....\n // http://www.garykessler.net/library/file_sigs.html\n // The metadata content for .jpgs might be located a lot farther down the file, so this\n // may pose problems in the future.\n // This cases on whether filePath is a remote URL or located on the machine\n const probeResult = fs.existsSync(filePath)\n ? imageProbe.sync(await readChunk(filePath, 0, 4100))\n : await imageProbe(data, { useElectronNet: false });\n if (!probeResult) {\n return;\n }\n\n const { width, height, type, mime } = probeResult;\n\n if (contentTypePattern && !mime.match(new RegExp(contentTypePattern))) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_CONTENT_TYPE',\n fieldPath,\n message: `field '${fieldPath}' should point to ${meta.contentTypeHuman} but the file at '${data}' has type ${type}`,\n data,\n meta,\n })\n );\n }\n\n if (dimensions && (dimensions.height !== height || dimensions.width !== width)) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_DIMENSIONS',\n fieldPath,\n message: `'${fieldPath}' should have dimensions ${dimensions.width}x${dimensions.height}, but the file at '${data}' has dimensions ${width}x${height}`,\n data,\n meta,\n })\n );\n }\n\n if (square && width !== height) {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'NOT_SQUARE',\n fieldPath,\n message: `image should be square, but the file at '${data}' has dimensions ${width}x${height}`,\n data,\n meta,\n })\n );\n }\n } catch {\n this.manualValidationErrors.push(\n new ValidationError({\n errorCode: 'INVALID_ASSET_URI',\n fieldPath,\n message: `cannot access file at '${data}'`,\n data,\n meta,\n })\n );\n }\n }\n }\n\n async _validateAssetAsync({ fieldPath, data, meta }: AssetField) {\n if (meta && meta.asset && data) {\n if (meta.contentTypePattern && meta.contentTypePattern.startsWith('^image')) {\n await this._validateImageAsync({ fieldPath, data, meta });\n }\n }\n }\n\n async validateProperty(fieldPath: string, data: any) {\n const subSchema = fieldPathToSchema(this.schema, fieldPath);\n this.ajv.validate(subSchema, data);\n\n if (subSchema.meta && subSchema.meta.asset) {\n await this._validateAssetAsync({ fieldPath, data, meta: subSchema.meta });\n }\n this._throwOnErrors();\n }\n\n validateName(name: string) {\n return this.validateProperty('name', name);\n }\n\n validateSlug(slug: string) {\n return this.validateProperty('slug', slug);\n }\n\n validateSdkVersion(version: string) {\n return this.validateProperty('sdkVersion', version);\n }\n\n validateIcon(iconPath: string) {\n return this.validateProperty('icon', iconPath);\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expo/schemer",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "Centralized scheme validation library for Expo",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,10 +27,11 @@
|
|
|
27
27
|
"@types/lodash": "^4.14.176"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"ajv": "^
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
30
|
+
"ajv": "^8.1.0",
|
|
31
|
+
"ajv-formats": "^2.0.2",
|
|
32
|
+
"json-schema-traverse": "^1.0.0",
|
|
33
|
+
"lodash": "^4.17.21",
|
|
34
|
+
"probe-image-size": "^7.1.0",
|
|
34
35
|
"read-chunk": "^3.2.0"
|
|
35
36
|
},
|
|
36
37
|
"publishConfig": {
|