UNPKG

ng-packagr

Version:

Compile and package Angular libraries in Angular Package Format (APF)

83 lines • 3.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateNgPackageSchema = validateNgPackageSchema; exports.validateNgPackageEntryPointSchema = validateNgPackageEntryPointSchema; exports.formatSchemaValidationErrors = formatSchemaValidationErrors; exports.getSchemaValidator = getSchemaValidator; const ajv_1 = __importDefault(require("ajv")); const log_1 = __importDefault(require("../utils/log")); /** Lazily initialized ajv validator instance. */ let ajvNgPackageSchemaValidator; let ajvNgPackageEntryPointSchemaValidator; /** * Validates the `ngPackageJson` value against the JSON schema using ajv. An error is thrown if * schema errors are found. * * @param ngPackageJson The value to validate. */ function validateNgPackageSchema(ngPackageJson) { const validate = (ajvNgPackageSchemaValidator ??= getSchemaValidator(require('../../../ng-package.schema.json'))); const isValid = validate(ngPackageJson); if (!isValid) { throw new Error(`Configuration doesn't match the required schema.\n${formatSchemaValidationErrors(validate.errors)}`); } } /** * Validates the `ngPackageJson` value against the JSON schema using ajv. An error is thrown if * schema errors are found. * * @param ngPackageJson The value to validate. */ function validateNgPackageEntryPointSchema(ngPackageJson) { const validate = (ajvNgPackageEntryPointSchemaValidator ??= getSchemaValidator(require('../../../ng-entrypoint.schema.json'))); const isValid = validate(ngPackageJson); if (!isValid) { throw new Error(`Configuration doesn't match the required schema.\n${formatSchemaValidationErrors(validate.errors)}`); } } /** Exported for testing. */ function formatSchemaValidationErrors(errors) { return errors .map(err => { if (err.keyword === 'additionalProperties') { const unknown = err.params.additionalProperty; // `parentSchema` is the schema that rejected the property, which ajv only attaches when // the validator was created with `verbose: true`. A schema that declares no `properties` // of its own has no options to offer. const known = Object.keys(err.parentSchema?.properties ?? {}); return (`Unknown option "${unknown}"${err.instancePath ? ` at "${err.instancePath}"` : ''}.` + (known.length ? ` Valid options are: ${known.join(', ')}.` : '')); } return `Data path ${JSON.stringify(err.instancePath)} ${err.message}.`; }) .join('\n'); } /** * Returns an initialized ajv validator for the ng-package JSON schema. * * Exported for testing. */ function getSchemaValidator(schema) { const _ajv = new ajv_1.default({ useDefaults: true, // Needed to list the valid options of the object an unknown option was found in. verbose: true, strict: false, // strict mode is enabled by default in JSON schema type definitions, which disallows the use of `useDefaults`. }); // Add handler for x-deprecated fields _ajv.addKeyword({ keyword: 'x-deprecated', validate: (schema, _data, _parentSchema, dataCxt) => { if (schema) { log_1.default.warn(`Option "${dataCxt.parentDataProperty}" is deprecated${typeof schema == 'string' ? ': ' + schema : '.'}`); } return true; }, errors: false, }); return _ajv.compile(schema); } //# sourceMappingURL=schema.js.map