Skip to content

Commit

Permalink
Merge branch 'feature/PB-32891_Entities-validating-null-in-anyOf-shou…
Browse files Browse the repository at this point in the history
…ld-use-nullable-schema-property' into 'develop'

Feature/pb 32891 entities validating null in any of should use nullable schema property

See merge request passbolt/passbolt-styleguide!1623
  • Loading branch information
Gamabunta57 committed May 14, 2024
2 parents 6556d5a + f8de722 commit 3173fd5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passbolt-styleguide",
"version": "4.8.0-alpha.10",
"version": "4.8.0-alpha.11",
"license": "AGPL-3.0",
"copyright": "Copyright 2023 Passbolt SA",
"description": "Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.",
Expand Down
12 changes: 11 additions & 1 deletion src/shared/models/entity/abstract/entitySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,21 @@ class EntitySchema {
continue;
}

// check if propery is null
if (dto?.[propName] === null) {
// the prop is explicitly null, is it explicitly nullable?
if ((schemaProps[propName]?.nullable) === true) {
result[propName] = null;
continue;
}
// @todo: else => props is not nullable and null we could set an error and then continue but it requires all schema to migrate to explicit "nullable": true
}

// Check if property is required
if (requiredProps.includes(propName)) {
if (!Object.prototype.hasOwnProperty.call(dto, propName)) {
validationError = EntitySchema.getOrInitEntityValidationError(name, validationError);
validationError.addError(propName, 'required', `The ${propName} is required.`, validationError);
validationError.addError(propName, 'required', `The ${propName} is required.`);
continue;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ class ApplicationEntity extends Entity {
"type": "object",
"required": ["remoteVersion", "currentVersion"],
"properties": {
"remoteVersion": {"anyOf": [{
"type": "string"
}, {
"type": "null"
}]},
"remoteVersion": {
"type": "string",
"nullable": true,
},
"currentVersion": {
"type": "string"
}
}
},
"latestVersion": {"anyOf": [{
"latestVersion": {
"type": "boolean",
}, {
"type": "null"
}]},
"nullable": true,
},
"schema": {
"type": "boolean"
},
Expand All @@ -81,11 +79,10 @@ class ApplicationEntity extends Entity {
"isSelfRegistrationPluginEnabled": {
"type": "boolean"
},
"selfRegistrationProvider": {"anyOf": [{
"type": "string"
}, {
"type": "null"
}]},
"selfRegistrationProvider": {
"type": "string",
"nullable": true,
},
"isRegistrationPublicRemovedFromPassbolt": {
"type": "boolean"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ class PasswordExpiryProSettingsEntity extends Entity {
"format": "uuid",
},
"default_expiry_period": {
"anyOf": [{
"type": "integer",
"gte": 1,
"lte": 999
}, {
"type": "null"
}]
"type": "integer",
"gte": 1,
"lte": 999,
"nullable": true,
},
"policy_override": {
"type": "boolean",
Expand Down
9 changes: 3 additions & 6 deletions src/shared/models/entity/ssoSettings/SsoSettingsEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ class SsoSettingsEntity extends Entity {
},
},
"provider": {
"anyOf": [{
"type": "string",
"enum": SsoSettingsEntity.AVAILABLE_PROVIDERS,
}, {
"type": "null"
}],
"type": "string",
"enum": SsoSettingsEntity.AVAILABLE_PROVIDERS,
"nullable": true,
},
"data": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("SsoSettingsEntity", () => {
const successScenarios = successValues.map(value => ({scenario: `with value "${value}}"`, value: value}));
const failingScenarios = failingValues.map(value => ({scenario: `with value "${value}}"`, value: value}));

assertEntityProperty.assert(SsoSettingsEntity, "provider", successScenarios, failingScenarios, "type");
assertEntityProperty.assert(SsoSettingsEntity, "provider", successScenarios, failingScenarios, "enum");
assertEntityProperty.nullable(SsoSettingsEntity, "provider");
assertEntityProperty.notRequired(SsoSettingsEntity, "provider");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe("PasswordExpirySettingsViewModel", () => {
},
{
dto: {default_expiry_period: -1},
expectedErrors: {default_expiry_period: {type: "The default_expiry_period does not match any of the supported types."}},
expectedErrors: {default_expiry_period: {type: "The default_expiry_period is not a valid integer."}},
},
]).describe("should validate the current data set with PasswordExpiryProSettingsEntity", scenario => {
it(`for: ${JSON.stringify(scenario.dto)}`, () => {
Expand Down
2 changes: 1 addition & 1 deletion test/assert/assertEntityProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const FAIL_LOCALE_SCENARIO = [
{scenario: "Incomplete", value: "fr"},
];
export const locale = (EntityClass, propertyName) => {
assert(EntityClass, propertyName, SUCCESS_LOCALE_SCENARIO, FAIL_LOCALE_SCENARIO, "type");
assert(EntityClass, propertyName, SUCCESS_LOCALE_SCENARIO, FAIL_LOCALE_SCENARIO, "pattern");
};

export const enumeration = (EntityClass, propertyName, successValues, failValues = []) => {
Expand Down

0 comments on commit 3173fd5

Please sign in to comment.