Skip to content

Commit

Permalink
fix: support additional values for Connector.isNullable
Browse files Browse the repository at this point in the history
Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
  • Loading branch information
achrinza committed Aug 28, 2022
1 parent 7d476a3 commit c88f4ac
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 23 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ jobs:
node_version: 16
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
with:
node-version: ${{ matrix.node-version }}
- name: Bootstrap project
run: npm ci --ignore-scripts
- name: Run tests
run: npm run-script test:ci
- name: Publish coverage report to Coveralls
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@9ba913c152ae4be1327bfb9085dc806cedb44057 # tag=v1.1.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: run-${{ matrix.os }}-node@${{ matrix.node-version }}
Expand All @@ -50,7 +50,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Coveralls finished
uses: coverallsapp/github-action@master
uses: coverallsapp/github-action@9ba913c152ae4be1327bfb9085dc806cedb44057 # tag=v1.1.3
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
Expand All @@ -59,9 +59,9 @@ jobs:
name: Code Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
- name: Use Node.js 16
uses: actions/setup-node@v3
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
with:
node-version: 16
- name: Bootstrap project
Expand All @@ -73,11 +73,11 @@ jobs:
name: Commit Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
with:
fetch-depth: 0
- name: Use Node.js 16
uses: actions/setup-node@v3
uses: actions/setup-node@2fddd8803e2f5c9604345a0b591c3020ee971a93 # tag=v3.4.1
with:
node-version: 16
- name: Bootstrap project
Expand All @@ -101,13 +101,12 @@ jobs:
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v3

uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@c7f292ea4f542c473194b33813ccd4c207a6c725 # tag=v2.1.21
with:
languages: 'javascript'
config-file: ./.github/codeql/codeql-config.yaml

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@c7f292ea4f542c473194b33813ccd4c207a6c725 # tag=v2.1.21
17 changes: 9 additions & 8 deletions lib/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
80 changes: 77 additions & 3 deletions test/connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
});
});

0 comments on commit c88f4ac

Please sign in to comment.