fix: support additional values for Connector.isNullable

Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
This commit is contained in:
Rifa Achrinza 2022-08-22 22:16:19 +08:00
parent 7d476a397f
commit d17b2414f5
2 changed files with 86 additions and 11 deletions

View File

@ -335,16 +335,17 @@ Connector.prototype.setIdValue = function(model, data, value) {
* @returns {boolean} true if nullable
*/
Connector.prototype.isNullable = function(prop) {
if (prop.required || prop.id) {
return false;
}
if (prop.nullable || prop['null'] || prop.allowNull) {
return true;
}
if (prop.nullable === false || prop['null'] === false ||
prop.allowNull === false) {
if (prop.required || prop.id)
return false;
const nullableFlagsValue = [prop.nullable, prop['null'], prop.allowNull];
const notNullableValues = [0, 'N', 'NO', false];
for (const flagValue of nullableFlagsValue) {
if (notNullableValues.includes(flagValue))
return false;
}
return true;
};

View File

@ -97,16 +97,90 @@ describe('Connector', () => {
});
it('should return undefined for non-existing nested property', () => {
const definition = connector.getPropertyDefinition('MyModel',
'someProp.innerArray.foo');
const definition = connector.getPropertyDefinition(
'MyModel',
'someProp.innerArray.foo',
);
// eslint-disable-next-line no-unused-expressions
expect(definition).to.be.undefined;
});
it('should preserve backward-compatibility for non-existing property', () => {
const definition = connector.getPropertyDefinition('MyModel', 'idontexist');
const definition = connector.getPropertyDefinition(
'MyModel',
'idontexist',
);
// eslint-disable-next-line no-unused-expressions
expect(definition).to.be.undefined;
});
});
describe('isNullable()', () => {
const nullableOverrideFlags = ['required', 'id'];
const nullableFlags = ['nullable', 'null', 'allowNull'];
const nullableValues = [1, 'Y', 'YES', true];
const notNullableValues = [0, 'N', 'NO', false];
for (const nullableOverrideFlag of nullableOverrideFlags) {
const propDefNullableOverridePlainSlice = {
[nullableOverrideFlag]: true,
};
it(`returns \`false\` for \`${JSON.stringify(
propDefNullableOverridePlainSlice,
)}`, () => {
const result = Connector.prototype.isNullable(
propDefNullableOverridePlainSlice,
);
// eslint-disable-next-line no-unused-expressions
expect(result).to.be.false;
});
for (const nullableFlag of nullableFlags) {
for (const nullableValue of nullableValues) {
const propDefNullableOverrideSlice = {
...propDefNullableOverridePlainSlice,
[nullableFlag]: nullableValue,
};
it(`returns \`false\` for \`${JSON.stringify(
propDefNullableOverrideSlice,
)}`, () => {
const result = Connector.prototype.isNullable(
propDefNullableOverrideSlice,
);
// eslint-disable-next-line no-unused-expressions
expect(result).to.be.false;
});
}
}
}
for (const nullableFlag of nullableFlags) {
for (const nullableValue of nullableValues) {
const propDefNullableSlice = {[nullableFlag]: nullableValue};
it(`returns \`true\` for \`${JSON.stringify(
propDefNullableSlice,
)}\``, () => {
const result = Connector.prototype.isNullable(propDefNullableSlice);
// eslint-disable-next-line no-unused-expressions
expect(result).to.be.true;
});
}
for (const notNullableValue of notNullableValues) {
const propDefNotNullableSlice = {[nullableFlag]: notNullableValue};
it(`returns \`false\` for \`${JSON.stringify(
propDefNotNullableSlice,
)}\``, () => {
const result = Connector.prototype.isNullable(
propDefNotNullableSlice,
);
// eslint-disable-next-line no-unused-expressions
expect(result).to.be.false;
});
}
}
});
});