UNPKG

@atlaskit/eslint-plugin-design-system

Version:

The essential plugin for use with the Atlassian Design System.

54 lines 1.34 kB
import Ajv from 'ajv'; const deprecatedSchema = { type: 'object', patternProperties: { '.+': { type: 'array', items: { type: 'object', properties: { moduleSpecifier: { type: 'string' }, namedSpecifiers: { type: 'array', items: { type: 'string' } }, actionableVersion: { type: 'string' } }, required: ['moduleSpecifier'], additionalProperites: false } } }, allowMatchingProperties: true }; export const getValidatedConfig = originalDeprecatedConfig => { let parsedDeprecatedConfig = {}; try { parsedDeprecatedConfig = JSON.parse(originalDeprecatedConfig); } catch (e) { const error = e; throw new Error(`Failed to parse JSON string: ${error.message}`); } const ajv = new Ajv({ allErrors: true }); const validate = ajv.compile(deprecatedSchema); const valid = validate(parsedDeprecatedConfig); if (!valid) { const { errors } = validate; if (errors && errors.length) { throw new Error(`Deprecated APIs config is invalid: ${errors}`); } else { throw new Error('Failed to validate deprecated APIs config with unknown error.'); } } return parsedDeprecatedConfig; };