structured-elements
Version:
A TypeScript package for modelling and validating data
51 lines • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isExpectation = void 0;
const constants_1 = require("../constants");
const array_1 = require("./array");
const expectationArray_1 = require("./expectationArray");
const referenceContainer_1 = require("./referenceContainer");
const stringKeyedRecord_1 = require("./stringKeyedRecord");
const isExpectation = (api, expectation, warn) => {
if ((0, referenceContainer_1.isReferenceContainer)(api, expectation)) {
return true;
}
if ((0, array_1.isArray)(expectation)) {
return (0, expectationArray_1.isExpectationArray)(api, expectation, warn);
}
if ((0, stringKeyedRecord_1.isStringKeyedRecord)(expectation)) {
if (expectation[constants_1.referenceToken]) {
return false;
}
const failures = [];
for (const key in expectation) {
if (!(0, exports.isExpectation)(api, expectation[key], warn)) {
failures.push({ key, value: expectation[key] });
}
}
if (failures.length === 0) {
return true;
}
if (warn && api.debugEnabled()) {
api.privateFunctions.debug(`StructuredElements#isExpectation found invalid record schema:`, {
subject: expectation,
failures,
});
}
return false;
}
if (typeof expectation === `string` ||
expectation === null ||
expectation === undefined) {
return true;
}
if (warn && api.debugEnabled()) {
api.privateFunctions.debug(`StructuredElements#isExpectation found invalid primitive expectation:`, {
subject: expectation,
typeofSubject: typeof expectation,
});
}
return false;
};
exports.isExpectation = isExpectation;
//# sourceMappingURL=expectation.js.map