"Checkbox"/option type MongoDB schema design - mongodb

I am looking to implement some type of checkbox functionality for settings on an account, e.g.
option:
value1: true
value2: false
value 3: false
My first thought was to have an array key named option and then have an array of Strings from an enum, like so:
key: { type: [String], required: true, enum: [
'value1', 'value2'
]}
and then I can check to see what options the users has in the array vs. the possible enum values...
But maybe I should do something like this;
key: {
value1: { type: Boolean, required: true, default: true },
value2: { type: Boolean, required: true, default: true }
}
I want this to be super customizable and flexible going forward, so I was wondering if anyone had any suggestions?

The second option is better. Much more easy to perform queries.

Related

Joi Validation Schema for a recursive type definition

I have a typescript type like
type FeatureSet = {
[key: string]: boolean | FeatureSet;
}
e.g. an example instance would be
{
label1: true,
label2: {
label3: false,
label4: false
},
someOtherKeyLabel: false
}
and want to create a Joi ValidationSchema for it but so far couldn't really manage to operate on abitrary key names. Anybody ideas?
Cheers
T

Why does collection.find_one({ _id: id }) always return nothing even with correct string id?

I am working on a MongoDB-based API webserver to learn Rust and I can't figure out why I can't get results:
use mongodb::bson::{doc, oid::ObjectId};
use mongodb::{error::Error, Database};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Job {
_id: ObjectId,
user_id: String,
organisation: String,
role: String,
}
async fn find_job_by_id(database: &Database, id: String) -> Result<Option<Job>, Error> {
database
.collection("jobs")
.find_one(Some(doc! { "_id": id }), None)
.await
}
I've tried printing out the id at every step and it always looks correct, but .find_one() always returns Ok(None). Any tips?
Turns out filters for ObjectId fields have to be specifically use ObjectId. You can't apply filters using strings as I was doing over there. This method is what fixed it:
let id = ObjectId::parse_str(id).unwrap();
Looks like the myths were true... rust is pretty harsh with type checking...

OpenAPI specification for request body containing class object [duplicate]

I'm having a hard time trying to figure out how I can nest models in OpenAPI 2.0.
Currently I have:
SomeModel:
properties:
prop1:
type: string
prop2:
type: integer
prop3:
type:
$ref: OtherModel
OtherModel:
properties:
otherProp:
type: string
I have tried many other ways:
prop3:
$ref: OtherModel
# or
prop3:
schema:
$ref: OtherModel
# or
prop3:
type:
schema:
$ref: OtherModel
None of the above seem to work.
However, with arrays works just fine:
prop3:
type: array
items:
$ref: OtherModel
The correct way to model it in OpenAPI 2.0 would be:
swagger: '2.0'
...
definitions:
SomeModel:
type: object
properties:
prop1:
type: string
prop2:
type: integer
prop3:
$ref: '#/definitions/OtherModel' # <-----
OtherModel:
type: object
properties:
otherProp:
type: string
If you use OpenAPI 3.0, models live in components/schemas instead of definitions:
openapi: 3.0.1
...
components:
schemas:
SomeModel:
type: object
properties:
prop1:
type: string
prop2:
type: integer
prop3:
$ref: '#/components/schemas/OtherModel' # <-----
OtherModel:
type: object
properties:
otherProp:
type: string
Remember to add type: object to your object schemas because the type is not inferred from other keywords.
Here's another trick that works. This solution applies to OpenAPI 3 – the latest version of the OpenAPI Specification as the point of answering this question.
Here's how:
Say, I have a User model with a State enum. I defined the State enum in a different schema and then referenced it in the User schema.
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
first_name:
type: string
last_name:
type: string
password:
type: string
format: password
state:
$ref: '#/components/schemas/State'
State:
type: string
description: List of States
enum:
- State 1
- State 2
- State 3
- State 4
- State 5
Note that the enums were represented as arrays here, if you would love to represent them as hashes, check out this solution on Representing enum property in hash.
That's all.
I hope this helps

What are the differences between 'CaseIterable' protocol's allCases and AllCases?

I am curious about the differences between 'CaseIterable' protocol's allCases and AllCases. According to Apple's document:
allCases : A collection of all values of this type. https://developer.apple.com/documentation/swift/caseiterable/2994869-allcases
AllCases : A type that can represent a collection of all values of this type. https://developer.apple.com/documentation/swift/caseiterable/2994868-allcases,
The most common usage when using 'CaseIterable' protocol seems to be allCases and I am aware of its actual usage in enum, but I can't find a solid example that explains the usage of AllCases. Any input will be appreciated.
Thank you.
First, go learn what associated types are.
Armed with that knowledge, you'll understand that AllCases just has to be a Collection of instances of a type—not specifically the Array of enumeration instances you'll generally experience it expressed as.
So, for a type with enumerable instances, whose order doesn't matter, you could use a Set, for example.
extension Bool: CaseIterable {
public static var allCases: Set<Self> { [true, false] }
}
extension Sequence where Element == Bool {
var containsBothBools: Bool { Bool.allCases.isSubset(of: self) }
}
[true].containsBothBools // false
[false].containsBothBools // false
[true, false, true, true, false, true].containsBothBools // true

How to have two raml properties mutually exclusive?

I have a type in raml1.0 with 4 properties and I need to implement this case:
Two properties out of the four exist only exclusively, so if one of them exists the other should not exist and if they both occur a propper error message is thrown to the user.
For example:
types:
TypeOne:
description: "Need the first two properties exist only mutually exclusively"
type: object
additionalProperties: false
properties:
Prop1:
description: "This is the first property"
type: string
required: true
Prop2:
description: "This should not exist if Prop1 exist"
type: String
required: true (only if Prop1 does not exist)
Prop3:
description: "This is optional if Prop1 exists"
type: string
required: false
Prop4:
description: "This is optional if Prop2 exists"
type: string
required: false
Any help is highly appreciated. BTW, each of these types is a complex object. I only simplified it here just for presentation.
Try this:
types:
Base:
properties:
Prop3:
description: "This is optional if Prop1 exists"
type: string
required: false
Prop4:
description: "This is optional if Prop2 exists"
type: string
required: false
TypeOne:
type: Base
additionalProperties: false
properties:
Prop1:
description: "This is the first property"
type: string
required: true
TypeTwo:
type: Base
additionalProperties: false
properties:
Prop2:
description: "This is the first property"
type: string
required: true
MainType:
type: TypeOne | TypeTwo
The documentation for union types can be found here: https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md/#union-type