Skip to content

Commit d17b241

Browse files
committed
fix: support additional values for Connector.isNullable
Signed-off-by: Rifa Achrinza <[email protected]>
1 parent 7d476a3 commit d17b241

File tree

2 files changed

+86
-11
lines changed

2 files changed

+86
-11
lines changed

lib/connector.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,17 @@ Connector.prototype.setIdValue = function(model, data, value) {
335335
* @returns {boolean} true if nullable
336336
*/
337337
Connector.prototype.isNullable = function(prop) {
338-
if (prop.required || prop.id) {
339-
return false;
340-
}
341-
if (prop.nullable || prop['null'] || prop.allowNull) {
342-
return true;
343-
}
344-
if (prop.nullable === false || prop['null'] === false ||
345-
prop.allowNull === false) {
338+
if (prop.required || prop.id)
346339
return false;
340+
341+
const nullableFlagsValue = [prop.nullable, prop['null'], prop.allowNull];
342+
const notNullableValues = [0, 'N', 'NO', false];
343+
344+
for (const flagValue of nullableFlagsValue) {
345+
if (notNullableValues.includes(flagValue))
346+
return false;
347347
}
348+
348349
return true;
349350
};
350351

test/connector.test.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,90 @@ describe('Connector', () => {
9797
});
9898

9999
it('should return undefined for non-existing nested property', () => {
100-
const definition = connector.getPropertyDefinition('MyModel',
101-
'someProp.innerArray.foo');
100+
const definition = connector.getPropertyDefinition(
101+
'MyModel',
102+
'someProp.innerArray.foo',
103+
);
102104
// eslint-disable-next-line no-unused-expressions
103105
expect(definition).to.be.undefined;
104106
});
105107

106108
it('should preserve backward-compatibility for non-existing property', () => {
107-
const definition = connector.getPropertyDefinition('MyModel', 'idontexist');
109+
const definition = connector.getPropertyDefinition(
110+
'MyModel',
111+
'idontexist',
112+
);
108113
// eslint-disable-next-line no-unused-expressions
109114
expect(definition).to.be.undefined;
110115
});
111116
});
117+
118+
describe('isNullable()', () => {
119+
const nullableOverrideFlags = ['required', 'id'];
120+
121+
const nullableFlags = ['nullable', 'null', 'allowNull'];
122+
123+
const nullableValues = [1, 'Y', 'YES', true];
124+
125+
const notNullableValues = [0, 'N', 'NO', false];
126+
127+
for (const nullableOverrideFlag of nullableOverrideFlags) {
128+
const propDefNullableOverridePlainSlice = {
129+
[nullableOverrideFlag]: true,
130+
};
131+
it(`returns \`false\` for \`${JSON.stringify(
132+
propDefNullableOverridePlainSlice,
133+
)}`, () => {
134+
const result = Connector.prototype.isNullable(
135+
propDefNullableOverridePlainSlice,
136+
);
137+
// eslint-disable-next-line no-unused-expressions
138+
expect(result).to.be.false;
139+
});
140+
141+
for (const nullableFlag of nullableFlags) {
142+
for (const nullableValue of nullableValues) {
143+
const propDefNullableOverrideSlice = {
144+
...propDefNullableOverridePlainSlice,
145+
[nullableFlag]: nullableValue,
146+
};
147+
it(`returns \`false\` for \`${JSON.stringify(
148+
propDefNullableOverrideSlice,
149+
)}`, () => {
150+
const result = Connector.prototype.isNullable(
151+
propDefNullableOverrideSlice,
152+
);
153+
// eslint-disable-next-line no-unused-expressions
154+
expect(result).to.be.false;
155+
});
156+
}
157+
}
158+
}
159+
160+
for (const nullableFlag of nullableFlags) {
161+
for (const nullableValue of nullableValues) {
162+
const propDefNullableSlice = {[nullableFlag]: nullableValue};
163+
it(`returns \`true\` for \`${JSON.stringify(
164+
propDefNullableSlice,
165+
)}\``, () => {
166+
const result = Connector.prototype.isNullable(propDefNullableSlice);
167+
// eslint-disable-next-line no-unused-expressions
168+
expect(result).to.be.true;
169+
});
170+
}
171+
172+
for (const notNullableValue of notNullableValues) {
173+
const propDefNotNullableSlice = {[nullableFlag]: notNullableValue};
174+
it(`returns \`false\` for \`${JSON.stringify(
175+
propDefNotNullableSlice,
176+
)}\``, () => {
177+
const result = Connector.prototype.isNullable(
178+
propDefNotNullableSlice,
179+
);
180+
// eslint-disable-next-line no-unused-expressions
181+
expect(result).to.be.false;
182+
});
183+
}
184+
}
185+
});
112186
});

0 commit comments

Comments
 (0)