UNPKG

@kintone/plugin-manifest-validator

Version:

[![npm version](https://badge.fury.io/js/%40kintone%2Fplugin-manifest-validator.svg)](https://badge.fury.io/js/%40kintone%2Fplugin-manifest-validator) ![Node.js version](https://img.shields.io/badge/dynamic/json.svg?url=https://raw.githubusercontent.com/k

154 lines 5.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ajv_1 = __importDefault(require("ajv")); const ajv_formats_1 = __importDefault(require("ajv-formats")); const bytes_1 = __importDefault(require("bytes")); const manifest_schema_json_1 = __importDefault(require("../manifest-schema.json")); const validate_https_url_1 = __importDefault(require("./validate-https-url")); const check_required_properties_1 = require("./check-required-properties"); /** * @param {Object} json * @param {Object=} options * @return {{valid: boolean, errors: Array<!Object>}} errors is null if valid */ exports.default = (json, options = {}) => { let relativePath = () => true; let maxFileSize; let fileExists; const warnings = []; if (typeof options.relativePath === "function") { relativePath = options.relativePath; } if (typeof options.maxFileSize === "function") { maxFileSize = options.maxFileSize; } if (typeof options.fileExists === "function") { fileExists = options.fileExists; } const ajv = new ajv_1.default({ allErrors: true, formats: { "http-url": (str) => (0, validate_https_url_1.default)(str, true), "https-url": (str) => (0, validate_https_url_1.default)(str), "relative-path": relativePath, }, }); (0, ajv_formats_1.default)(ajv, { mode: "fast", formats: ["uri"] }); const validateMaxFileSize = (schema, filePath) => { var _a; // schema: max file size like "512KB" or 123 (in bytes) // data: path to the file if (maxFileSize === undefined) { return true; } const maxBytes = bytes_1.default.parse(schema); const result = maxFileSize(maxBytes, filePath); const defaultMessage = `file size should be <= ${schema}`; if (result === false) { validateMaxFileSize.errors = [ { keyword: "maxFileSize", params: { limit: maxBytes, }, message: defaultMessage, }, ]; return false; } if (typeof result === "object" && !result.valid) { validateMaxFileSize.errors = [ { keyword: "maxFileSize", params: { limit: maxBytes, }, message: (_a = result.message) !== null && _a !== void 0 ? _a : defaultMessage, }, ]; return false; } return true; }; const validateFileExists = (schema, filePath) => { var _a; if (fileExists === undefined || !schema) { return true; } const result = fileExists(filePath); const defaultMessage = `file should exist ("${filePath}")`; if (result === false) { validateFileExists.errors = [ { keyword: "fileExists", message: defaultMessage, }, ]; return false; } if (typeof result === "object" && !result.valid) { validateFileExists.errors = [ { keyword: "fileExists", message: (_a = result.message) !== null && _a !== void 0 ? _a : defaultMessage, }, ]; return false; } return true; }; const validateRequiredProperties = (schema, data) => { if (!data || data.length === 0) { return true; } const errors = (0, check_required_properties_1.checkRequiredProperties)(json, schema); if (errors.length === 0) { return true; } if (schema.warn) { warnings.push(...errors.map((error) => { return { message: error }; })); return true; } validateRequiredProperties.errors = errors.map((error) => ({ keyword: "requiredProperties", message: error, })); return false; }; ajv.addKeyword({ keyword: "maxFileSize", validate: validateMaxFileSize, }); ajv.addKeyword({ keyword: "fileExists", validate: validateFileExists, }); ajv.addKeyword({ keyword: "requiredProperties", validate: validateRequiredProperties, }); const validate = ajv.compile(manifest_schema_json_1.default); const valid = validate(json); return { valid, errors: transformErrors(validate.errors), warnings: warnings.length === 0 ? null : warnings, }; }; /** * @param {undefined|null|Array<Object>} errors * @return {null|Array<Object>} shallow copy of the input or null */ const transformErrors = (errors) => { if (!errors) { return null; } // shallow copy return errors.slice(); }; //# sourceMappingURL=index.js.map