@coat/cli
Version:
TODO: See #3
45 lines (44 loc) • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleUnknownProperties = handleUnknownProperties;
var _chalk = _interopRequireDefault(require("chalk"));
var _findPotentialPropertyMatch = require("./find-potential-property-match");
var _formatPropertyPath = require("./format-property-path");
var _validationIssue = require("./validation-issue");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Validation helper to generate issues for unknown properties in an object.
*
* @param input.allOptionalProps Potential optional properties of an object,
* required properties should be handled directly in the specific validation methods.
* @param input.unknownProps Additional props that have been declared and don't correspond
* to the known schema.
* @param input.declaredProps All properties that are already declared on the object,
* to exclude suggestions for already set properties.
* @param input.propertyPrefixPath The path to the current property, to prefix the
* formatted result if necessary.
*/
function* handleUnknownProperties({
allOptionalProps,
unknownProps,
declaredProps,
propertyPrefixPath = []
}) {
// Remove already declared properties from potential property matches
const possibleProps = allOptionalProps.filter(prop => !declaredProps.includes(prop));
for (const prop of unknownProps) {
const bestPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)(prop, possibleProps);
const propertyPath = [...propertyPrefixPath, prop];
const messageParts = [`${(0, _formatPropertyPath.formatPropertyPath)(propertyPath)} Unknown property.`];
if (bestPropertyMatch) {
messageParts.push((0, _chalk.default)` Did you mean {magenta ${bestPropertyMatch}}?`);
}
yield {
type: _validationIssue.ValidationIssueType.Warning,
message: messageParts.join(""),
propertyPath
};
}
}