Skip to content

Commit c88f4ac

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

File tree

3 files changed

+97
-23
lines changed

3 files changed

+97
-23
lines changed

.github/workflows/continuous-integration.yaml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ jobs:
2828
node_version: 16
2929
fail-fast: false
3030
steps:
31-
- uses: actions/checkout@v3
31+
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
3232
- name: Use Node.js ${{ matrix.node-version }}
33-
uses: actions/setup-node@v3
33+
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
3434
with:
3535
node-version: ${{ matrix.node-version }}
3636
- name: Bootstrap project
3737
run: npm ci --ignore-scripts
3838
- name: Run tests
3939
run: npm run-script test:ci
4040
- name: Publish coverage report to Coveralls
41-
uses: coverallsapp/github-action@master
41+
uses: coverallsapp/github-action@9ba913c152ae4be1327bfb9085dc806cedb44057 # tag=v1.1.3
4242
with:
4343
github-token: ${{ secrets.GITHUB_TOKEN }}
4444
flag-name: run-${{ matrix.os }}-node@${{ matrix.node-version }}
@@ -50,7 +50,7 @@ jobs:
5050
runs-on: ubuntu-latest
5151
steps:
5252
- name: Coveralls finished
53-
uses: coverallsapp/github-action@master
53+
uses: coverallsapp/github-action@9ba913c152ae4be1327bfb9085dc806cedb44057 # tag=v1.1.3
5454
with:
5555
github-token: ${{ secrets.github_token }}
5656
parallel-finished: true
@@ -59,9 +59,9 @@ jobs:
5959
name: Code Lint
6060
runs-on: ubuntu-latest
6161
steps:
62-
- uses: actions/checkout@v3
62+
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
6363
- name: Use Node.js 16
64-
uses: actions/setup-node@v3
64+
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
6565
with:
6666
node-version: 16
6767
- name: Bootstrap project
@@ -73,11 +73,11 @@ jobs:
7373
name: Commit Lint
7474
runs-on: ubuntu-latest
7575
steps:
76-
- uses: actions/checkout@v3
76+
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
7777
with:
7878
fetch-depth: 0
7979
- name: Use Node.js 16
80-
uses: actions/setup-node@v3
80+
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
8181
with:
8282
node-version: 16
8383
- name: Bootstrap project
@@ -101,13 +101,12 @@ jobs:
101101
security-events: write
102102
steps:
103103
- name: Checkout repository
104-
uses: actions/checkout@v3
105-
104+
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
106105
- name: Initialize CodeQL
107-
uses: github/codeql-action/init@v1
106+
uses: github/codeql-action/init@c7f292ea4f542c473194b33813ccd4c207a6c725 # tag=v2.1.21
108107
with:
109108
languages: 'javascript'
110109
config-file: ./.github/codeql/codeql-config.yaml
111110

112111
- name: Perform CodeQL Analysis
113-
uses: github/codeql-action/analyze@v1
112+
uses: github/codeql-action/analyze@c7f292ea4f542c473194b33813ccd4c207a6c725 # tag=v2.1.21

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)