Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move name validation to route deserialization #253

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/api-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ serde = { version = "1.0.130", features = ["derive"] }
bson = { version = "2.0.0", optional = true }
derive_more = "0.99.17"
serde_json = "1.0.59"
rustrict = "0.1.30"
regex = "1"
lazy_static = "1.4.0"
lettre = {version = "0.11.2", features = ["smtp-transport", "builder"]}
# TODO: make this a dev dependency or a feature
ts-rs = { version = "7.0", features = ["uuid-impl", "serde-compat"] }

Expand Down
3 changes: 2 additions & 1 deletion crates/api-common/bindings/AuthorizedServiceHost.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { ServiceHostScope } from "./ServiceHostScope";

export interface AuthorizedServiceHost { url: string, id: string, visibility: ServiceHostScope, }
export interface AuthorizedServiceHost { url: string, id: Name, visibility: ServiceHostScope, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/CreateGroupData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { ServiceHost } from "./ServiceHost";

export interface CreateGroupData { name: string, servicesHosts?: Array<ServiceHost>, }
export interface CreateGroupData { name: Name, servicesHosts?: Array<ServiceHost>, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/CreateLibraryData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";

export interface CreateLibraryData { name: string, notes: string, blocks: string, }
export interface CreateLibraryData { name: Name, notes: string, blocks: string, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/CreateProjectData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ClientId } from "./ClientId";
import type { Name } from "./Name";
import type { RoleData } from "./RoleData";
import type { SaveState } from "./SaveState";

export interface CreateProjectData { owner?: string, name: string, roles?: Array<RoleData>, clientId?: ClientId, saveState?: SaveState, }
export interface CreateProjectData { owner?: string, name: Name, roles?: Array<RoleData>, clientId?: ClientId, saveState?: SaveState, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/Credentials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";

export type Credentials = { "Snap": { username: string, password: string, } } | { "NetsBlox": { username: string, password: string, } };
export type Credentials = { "Snap": { username: string, password: string, } } | { "NetsBlox": { username: Name, password: string, } };
3 changes: 3 additions & 0 deletions crates/api-common/bindings/EmailValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type EmailValidator = null;
3 changes: 3 additions & 0 deletions crates/api-common/bindings/GroupNameValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type GroupNameValidator = null;
3 changes: 2 additions & 1 deletion crates/api-common/bindings/LibraryMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { PublishState } from "./PublishState";

export interface LibraryMetadata { owner: string, name: string, notes: string, state: PublishState, }
export interface LibraryMetadata { owner: string, name: Name, notes: string, state: PublishState, }
3 changes: 3 additions & 0 deletions crates/api-common/bindings/LibraryNameValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type LibraryNameValidator = null;
3 changes: 3 additions & 0 deletions crates/api-common/bindings/Name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type Name = string;
3 changes: 2 additions & 1 deletion crates/api-common/bindings/NewUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { GroupId } from "./GroupId";
import type { Name } from "./Name";
import type { UserRole } from "./UserRole";

export interface NewUser { username: string, email: string, password?: string, groupId?: GroupId, role?: UserRole, }
export interface NewUser { username: Name, email: Name, password?: string, groupId?: GroupId, role?: UserRole, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/Project.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { ProjectId } from "./ProjectId";
import type { PublishState } from "./PublishState";
import type { RoleData } from "./RoleData";
import type { RoleId } from "./RoleId";
import type { SaveState } from "./SaveState";

export interface Project { id: ProjectId, owner: string, name: string, updated: any, state: PublishState, collaborators: Array<string>, originTime: any, saveState: SaveState, roles: Record<RoleId, RoleData>, }
export interface Project { id: ProjectId, owner: string, name: Name, updated: any, state: PublishState, collaborators: Array<string>, originTime: any, saveState: SaveState, roles: Record<RoleId, RoleData>, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/ProjectMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { NetworkTraceMetadata } from "./NetworkTraceMetadata";
import type { ProjectId } from "./ProjectId";
import type { PublishState } from "./PublishState";
import type { RoleId } from "./RoleId";
import type { RoleMetadata } from "./RoleMetadata";
import type { SaveState } from "./SaveState";

export interface ProjectMetadata { id: ProjectId, owner: string, name: string, updated: any, state: PublishState, collaborators: Array<string>, networkTraces: Array<NetworkTraceMetadata>, originTime: any, saveState: SaveState, roles: Record<RoleId, RoleMetadata>, }
export interface ProjectMetadata { id: ProjectId, owner: string, name: Name, updated: any, state: PublishState, collaborators: Array<string>, networkTraces: Array<NetworkTraceMetadata>, originTime: any, saveState: SaveState, roles: Record<RoleId, RoleMetadata>, }
3 changes: 3 additions & 0 deletions crates/api-common/bindings/ProjectNameValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type ProjectNameValidator = null;
3 changes: 2 additions & 1 deletion crates/api-common/bindings/RoleData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";

export interface RoleData { name: string, code: string, media: string, }
export interface RoleData { name: Name, code: string, media: string, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/RoleMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";

export interface RoleMetadata { name: string, code: string, media: string, }
export interface RoleMetadata { name: Name, code: string, media: string, }
3 changes: 3 additions & 0 deletions crates/api-common/bindings/RoleNameValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type RoleNameValidator = null;
3 changes: 2 additions & 1 deletion crates/api-common/bindings/RoleState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { OccupantState } from "./OccupantState";

export interface RoleState { name: string, occupants: Array<OccupantState>, }
export interface RoleState { name: Name, occupants: Array<OccupantState>, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/RoomState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";
import type { ProjectId } from "./ProjectId";
import type { RoleId } from "./RoleId";
import type { RoleState } from "./RoleState";

export interface RoomState { id: ProjectId, owner: string, name: string, roles: Record<RoleId, RoleState>, collaborators: Array<string>, version: bigint, }
export interface RoomState { id: ProjectId, owner: string, name: Name, roles: Record<RoleId, RoleState>, collaborators: Array<string>, version: bigint, }
3 changes: 3 additions & 0 deletions crates/api-common/bindings/ServiceIDValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type ServiceIDValidator = null;
3 changes: 2 additions & 1 deletion crates/api-common/bindings/UpdateGroupData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { Name } from "./Name";

export interface UpdateGroupData { name: string, }
export interface UpdateGroupData { name: Name, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/UpdateProjectData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ClientId } from "./ClientId";
import type { Name } from "./Name";

export interface UpdateProjectData { name: string, clientId?: ClientId, }
export interface UpdateProjectData { name: Name, clientId?: ClientId, }
3 changes: 2 additions & 1 deletion crates/api-common/bindings/UpdateRoleData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ClientId } from "./ClientId";
import type { Name } from "./Name";

export interface UpdateRoleData { name: string, clientId?: ClientId, }
export interface UpdateRoleData { name: Name, clientId?: ClientId, }
3 changes: 3 additions & 0 deletions crates/api-common/bindings/UsernameValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type UsernameValidator = null;
28 changes: 27 additions & 1 deletion crates/api-common/src/bson.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
oauth, FriendInvite, FriendLinkState, GroupId, InvitationState, LinkedAccount, MagicLinkId,
ProjectId, PublishState, RoleMetadata, SaveState, ServiceHost, ServiceHostScope, UserRole,
Name, ProjectId, PublishState, RoleMetadata, SaveState, ServiceHost, ServiceHostScope,
UserRole,
};
use bson::{doc, Bson, DateTime};

Expand Down Expand Up @@ -93,6 +94,12 @@ impl From<GroupId> for Bson {
}
}

impl<T: crate::Validate> From<Name<T>> for Bson {
fn from(name: Name<T>) -> Bson {
Bson::String(name.to_string())
}
}

impl From<InvitationState> for Bson {
fn from(state: InvitationState) -> Bson {
match state {
Expand Down Expand Up @@ -167,6 +174,8 @@ impl From<MagicLinkId> for Bson {

#[cfg(test)]
mod tests {
use bson::to_bson;

use super::*;

#[test]
Expand All @@ -184,4 +193,21 @@ mod tests {
let scope: Result<ServiceHostScope, _> = serde_json::from_str(&data);
assert!(scope.is_ok());
}
#[test]
fn test_bson_serialize_update_project_data() {
let new_name = "new project";
let name = crate::ProjectName::new(new_name.to_string());

// Serialize the struct to BSON
let serialized = to_bson(&name).expect("Failed to serialize to BSON");
println!("Serialized BSON: {:?}", serialized);

// Deserialize the BSON back to the struct
let deserialized: crate::ProjectName =
bson::from_bson(serialized).expect("Failed to deserialize from BSON");
println!("Deserialized struct: {:?}", deserialized);

// Check that the deserialized value matches the original
assert_eq!(deserialized.as_str(), new_name);
}
}
Loading
Loading