passbolt-styleguide
Version:
Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.
234 lines (209 loc) • 5.15 kB
JavaScript
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 4.9.0
*/
import { v4 as uuid } from "uuid";
import EntityV2 from "./entityV2";
import EntityValidationError from "./entityValidationError";
import EntityV2Collection from "./entityV2Collection";
export class TestEntityV2 extends EntityV2 {
static getSchema() {
return {
type: "object",
required: ["name"],
properties: {
id: {
type: "string",
format: "uuid",
nullable: true,
},
name: {
type: "string",
nullable: true,
},
number: {
type: "number",
nullable: true,
},
integer: {
type: "integer",
nullable: true,
},
boolean: {
type: "boolean",
nullable: true,
},
object: {
type: "object",
},
array: {
type: "array",
items: {
type: "string",
},
},
associated_entity: TestAssociatedEntityV2.getSchema(),
associated_collection: TestAssociatedCollection.getSchema(),
},
};
}
static get associations() {
return {
associated_entity: TestAssociatedEntityV2,
associated_collection: TestAssociatedCollection,
};
}
marshall() {
if (this._props?.name === "K4r3n") {
this._props.name = "Karen";
}
super.marshall();
}
// eslint-disable-next-line no-unused-vars
validateBuildRules(options = {}) {
if (this.name === "Karen") {
const error = new EntityValidationError();
error.addError("name", "karen", "I want to see the manager");
throw error;
}
}
get id() {
return this._props.id || null;
}
set id(id) {
this._props.id = id;
}
get name() {
return this._props.name;
}
set name(name) {
this._props.name = name;
}
get associatedEntity() {
return this._associatedEntity;
}
get associatedCollection() {
return this._associatedCollection;
}
/**
* Return a DTO
*
* @param {object} [contain] optional
* @returns {object}
*/
toDto(contain) {
const result = Object.assign({}, this._props);
if (!contain) {
return result;
}
if (this.associatedEntity && contain.associated_entity) {
result.associated_entity = this.associatedEntity.toDto();
}
if (this.associatedCollection && contain.associated_collection) {
result.associated_collection = this.associatedCollection.toDto();
}
return result;
}
/**
* TestEntityV2.ALL_CONTAIN_OPTIONS
* @returns {object} all contain options that can be used in toDto()
*/
static get ALL_CONTAIN_OPTIONS() {
return { associated_entity: true, associated_collection: true };
}
}
export class TestAssociatedEntityV2 extends EntityV2 {
static getSchema() {
return {
type: "object",
required: ["id"],
properties: {
id: {
type: "string",
format: "uuid",
},
},
};
}
get id() {
return this._props.id || null;
}
}
export class TestWithAssociationEntityV2 extends EntityV2 {
static getSchema() {
return {
type: "object",
required: ["name", "associated_entity"],
properties: {
name: {
type: "string",
},
associated_entity: TestAssociatedEntityV2.getSchema(),
},
};
}
static get associations() {
return {
associated_entity: TestAssociatedEntityV2,
};
}
get name() {
return this._props.name;
}
set name(name) {
this._props.name = name;
}
get associatedEntity() {
return this._associatedEntity;
}
}
export class TestAssociatedCollection extends EntityV2Collection {
/**
* @inheritDoc
*/
get entityClass() {
return TestAssociatedEntityV2;
}
/**
* Get metadata private keys collection schema
*
* @returns {Object} schema
*/
static getSchema() {
return {
type: "array",
items: TestAssociatedEntityV2.getSchema(),
};
}
}
export const minimalTestEntityV2Dto = (data) => ({
name: "test name",
...data,
});
export const defaultTestEntityV2Dto = (data) => ({
id: uuid(),
name: "test name",
number: Math.random(),
integer: Math.floor(Math.random() * 100),
boolean: Math.random() < 0.5,
object: {
foo: uuid(),
},
array: [uuid(), uuid()],
associated_entity: defaultAssociatedTestEntityV2Dto(),
associated_collection: [defaultAssociatedTestEntityV2Dto()],
...data,
});
export const defaultAssociatedTestEntityV2Dto = (data) => ({
id: uuid(),
...data,
});