Added falsy fields to be automatically excluded with warn being emitted

This commit is contained in:
Alexander Cerutti
2022-02-22 00:04:07 +01:00
parent 7ccbdf9f80
commit 27511806a8
2 changed files with 20 additions and 1 deletions

View File

@@ -68,6 +68,18 @@ describe("FieldsArray", () => {
expect(fa.length).toBe(1); expect(fa.length).toBe(1);
}); });
it("should log a warning if input items contain undefined and, then, ignore it", () => {
console.warn = jasmine.createSpy("log");
fa.push(undefined, { key: "t1", value: "v1" });
expect(console.warn).toHaveBeenCalledWith(
Messages.FIELDS.INVALID.replace("%s", "undefined"),
);
expect(fa.length).toBe(1);
});
}); });
describe("pop", () => { describe("pop", () => {

View File

@@ -68,6 +68,11 @@ function registerWithValidation(
let validItems: Schemas.Field[] = []; let validItems: Schemas.Field[] = [];
for (const field of items) { for (const field of items) {
if (!field) {
console.warn(Messages.format(Messages.FIELDS.INVALID, field));
continue;
}
try { try {
Schemas.assertValidity( Schemas.assertValidity(
Schemas.Field, Schemas.Field,
@@ -76,7 +81,9 @@ function registerWithValidation(
); );
if (instance[sharedKeysPoolSymbol].has(field.key)) { if (instance[sharedKeysPoolSymbol].has(field.key)) {
throw Messages.format(Messages.FIELDS.REPEATED_KEY, field.key); throw new TypeError(
Messages.format(Messages.FIELDS.REPEATED_KEY, field.key),
);
} }
instance[sharedKeysPoolSymbol].add(field.key); instance[sharedKeysPoolSymbol].add(field.key);