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K,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"]}
\No newline at end of file