@expo/schemer 1.4.0 → 1.4.3

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/Util.js CHANGED
@@ -7,7 +7,7 @@ exports.fieldPathToSchema = exports.schemaPointerToFieldPath = exports.fieldPath
7
7
  const get_1 = __importDefault(require("lodash/get"));
8
8
  const zip_1 = __importDefault(require("lodash/zip"));
9
9
  const fieldPathToSchemaPath = (fieldPath) => {
10
- return zip_1.default(fieldPath.split('.').fill('properties'), fieldPath.split('.')).flat().join('.');
10
+ return (0, zip_1.default)(fieldPath.split('.').fill('properties'), fieldPath.split('.')).flat().join('.');
11
11
  };
12
12
  exports.fieldPathToSchemaPath = fieldPathToSchemaPath;
13
13
  // Assumption: used only for jsonPointer returned from traverse
@@ -20,7 +20,7 @@ const schemaPointerToFieldPath = (jsonPointer) => {
20
20
  };
21
21
  exports.schemaPointerToFieldPath = schemaPointerToFieldPath;
22
22
  const fieldPathToSchema = (schema, fieldPath) => {
23
- return get_1.default(schema, exports.fieldPathToSchemaPath(fieldPath));
23
+ return (0, get_1.default)(schema, (0, exports.fieldPathToSchemaPath)(fieldPath));
24
24
  };
25
25
  exports.fieldPathToSchema = fieldPathToSchema;
26
26
  //# sourceMappingURL=Util.js.map
package/build/Util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Util.js","sourceRoot":"","sources":["../src/Util.ts"],"names":[],"mappings":";;;;;;AAAA,qDAA6B;AAC7B,qDAA6B;AAEtB,MAAM,qBAAqB,GAAG,CAAC,SAAiB,EAAE,EAAE;IACzD,OAAO,aAAG,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7F,CAAC,CAAC;AAFW,QAAA,qBAAqB,yBAEhC;AACF,+DAA+D;AACxD,MAAM,wBAAwB,GAAG,CAAC,WAAmB,EAAE,EAAE;IAC9D,OAAO,WAAW;SACf,KAAK,CAAC,GAAG,CAAC;SACV,KAAK,CAAC,CAAC,CAAC;SACR,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,YAAY,CAAC;SAC/B,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC,CAAC;AANW,QAAA,wBAAwB,4BAMnC;AAEK,MAAM,iBAAiB,GAAG,CAAC,MAAc,EAAE,SAAiB,EAAE,EAAE;IACrE,OAAO,aAAG,CAAC,MAAM,EAAE,6BAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;AACvD,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B","sourcesContent":["import get from 'lodash/get';\nimport zip from 'lodash/zip';\n\nexport const fieldPathToSchemaPath = (fieldPath: string) => {\n return zip(fieldPath.split('.').fill('properties'), fieldPath.split('.')).flat().join('.');\n};\n// Assumption: used only for jsonPointer returned from traverse\nexport const schemaPointerToFieldPath = (jsonPointer: string) => {\n return jsonPointer\n .split('/')\n .slice(2)\n .filter(e => e !== 'properties')\n .join('.');\n};\n\nexport const fieldPathToSchema = (schema: object, fieldPath: string) => {\n return get(schema, fieldPathToSchemaPath(fieldPath));\n};\n"]}
1
+ {"version":3,"file":"Util.js","sourceRoot":"","sources":["../src/Util.ts"],"names":[],"mappings":";;;;;;AAAA,qDAA6B;AAC7B,qDAA6B;AAEtB,MAAM,qBAAqB,GAAG,CAAC,SAAiB,EAAE,EAAE;IACzD,OAAO,IAAA,aAAG,EAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC7F,CAAC,CAAC;AAFW,QAAA,qBAAqB,yBAEhC;AACF,+DAA+D;AACxD,MAAM,wBAAwB,GAAG,CAAC,WAAmB,EAAE,EAAE;IAC9D,OAAO,WAAW;SACf,KAAK,CAAC,GAAG,CAAC;SACV,KAAK,CAAC,CAAC,CAAC;SACR,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,YAAY,CAAC;SAC/B,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC,CAAC;AANW,QAAA,wBAAwB,4BAMnC;AAEK,MAAM,iBAAiB,GAAG,CAAC,MAAc,EAAE,SAAiB,EAAE,EAAE;IACrE,OAAO,IAAA,aAAG,EAAC,MAAM,EAAE,IAAA,6BAAqB,EAAC,SAAS,CAAC,CAAC,CAAC;AACvD,CAAC,CAAC;AAFW,QAAA,iBAAiB,qBAE5B","sourcesContent":["import get from 'lodash/get';\nimport zip from 'lodash/zip';\n\nexport const fieldPathToSchemaPath = (fieldPath: string) => {\n return zip(fieldPath.split('.').fill('properties'), fieldPath.split('.')).flat().join('.');\n};\n// Assumption: used only for jsonPointer returned from traverse\nexport const schemaPointerToFieldPath = (jsonPointer: string) => {\n return jsonPointer\n .split('/')\n .slice(2)\n .filter(e => e !== 'properties')\n .join('.');\n};\n\nexport const fieldPathToSchema = (schema: object, fieldPath: string) => {\n return get(schema, fieldPathToSchemaPath(fieldPath));\n};\n"]}
package/build/index.js CHANGED
@@ -126,12 +126,12 @@ class Schemer {
126
126
  }
127
127
  async _validateAssetsAsync(data) {
128
128
  const assets = [];
129
- json_schema_traverse_1.default(this.schema, { allKeys: true }, (subSchema, jsonPointer, a, b, c, d, property) => {
129
+ (0, json_schema_traverse_1.default)(this.schema, { allKeys: true }, (subSchema, jsonPointer, a, b, c, d, property) => {
130
130
  if (property && subSchema.meta && subSchema.meta.asset) {
131
- const fieldPath = Util_1.schemaPointerToFieldPath(jsonPointer);
131
+ const fieldPath = (0, Util_1.schemaPointerToFieldPath)(jsonPointer);
132
132
  assets.push({
133
133
  fieldPath,
134
- data: get_1.default(data, fieldPath),
134
+ data: (0, get_1.default)(data, fieldPath),
135
135
  meta: subSchema.meta,
136
136
  });
137
137
  }
@@ -151,8 +151,8 @@ class Schemer {
151
151
  // may pose problems in the future.
152
152
  // This cases on whether filePath is a remote URL or located on the machine
153
153
  const probeResult = fs_1.default.existsSync(filePath)
154
- ? probe_image_size_1.default.sync(await read_chunk_1.default(filePath, 0, 4100))
155
- : await probe_image_size_1.default(data, { useElectronNet: false });
154
+ ? probe_image_size_1.default.sync(await (0, read_chunk_1.default)(filePath, 0, 4100))
155
+ : await (0, probe_image_size_1.default)(data, { useElectronNet: false });
156
156
  if (!probeResult) {
157
157
  return;
158
158
  }
@@ -185,7 +185,7 @@ class Schemer {
185
185
  }));
186
186
  }
187
187
  }
188
- catch (e) {
188
+ catch {
189
189
  this.manualValidationErrors.push(new Error_1.ValidationError({
190
190
  errorCode: 'INVALID_ASSET_URI',
191
191
  fieldPath,
@@ -204,7 +204,7 @@ class Schemer {
204
204
  }
205
205
  }
206
206
  async validateProperty(fieldPath, data) {
207
- const subSchema = Util_1.fieldPathToSchema(this.schema, fieldPath);
207
+ const subSchema = (0, Util_1.fieldPathToSchema)(this.schema, fieldPath);
208
208
  this.ajv.validate(subSchema, data);
209
209
  if (subSchema.meta && subSchema.meta.asset) {
210
210
  await this._validateAssetAsync({ fieldPath, data, meta: subSchema.meta });
@@ -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,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AE,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 (e) {\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,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,C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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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expo/schemer",
3
- "version": "1.4.0",
3
+ "version": "1.4.3",
4
4
  "description": "Centralized scheme validation library for Expo",
5
5
  "main": "./build/index.js",
6
6
  "scripts": {
@@ -27,7 +27,7 @@
27
27
  "@types/lodash": "^4.14.176"
28
28
  },
29
29
  "dependencies": {
30
- "ajv": "^5.2.2",
30
+ "ajv": "^6.12.6",
31
31
  "json-schema-traverse": "0.3.1",
32
32
  "lodash": "^4.17.19",
33
33
  "probe-image-size": "~6.0.0",