Joi Validation Schema for a recursive type definition - joi

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

Related

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

NSPredicate for filtering array of primitives field in Realm 3

We have to store elements in Realm that have array of string field.
To do this we have to implement small workaround:
class RealmString: Object {
#objc dynamic var value = ""
override init(value: Any) {
super.init(value: [value])
}
required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
required init() {
super.init()
}
required init(value: Any, schema: RLMSchema) {
super.init(value: [value], schema: schema)
}
}
class Realm2Element: Object {
let tags = List<RealmString>()
}
As a result:
CONTAINS ALL OF condition looks like this:
ANY tags.value == "tag0" AND ANY tags.value == "tag1" AND ANY tags.value == "tag2"
CONTAINS ANY OF condition looks like this:
ANY tags.value IN {"tag0", "tag1", "tag2"}
Realm3 suppors array of primitives. We removed .value keyPath from condition. But we have got error when applied these filters for Realm3Element:
class Realm3Element: Object {
let tags = List<String>()
}
For CONTAINS ALL OF condition:
'Invalid value', reason: 'Expected object of type (null) for property 'tags' on object of type 'Realm3Element', but received: tag0'
For CONTAINS ANY OF condition:
'Expected object of type (null) in IN clause for property 'tags' on object of type 'Realm3Element', but received: tag0'
We run through the all possible variations, but couldn't find any solution.
Does Realm3 support filtering by array of primitives field?
You can't achieve your goals using predicate with Realm because Realm have a lot of limitations using Predicates but you can use this way as a workaround
let filterArray = ["tag0","tag1"]
Array(realm.objects(Realm3Element.self)).filter({$0.tags.sorted().joined().contains(filterArray.sorted().joined())})
If you want a perfect solution tracking this issue #5334

Swift generics with variables

I have the following issue with using generics in my application. As seen below my GeneralUpdate class enforces a type conformance of T to the ReadingClass, but I cannot assign variable of type Reading in the initializer (marked in the GeneralUpdate class as a comment)
class Reading {
var readingDate: Date!
var enteredDate: Date!
init(readingDate: Date, endDate: Date) {
self.readingDate = readingDate
self.enteredDate = endDate
}
}
class GeneralUpdate<T: Reading> {
var readingType: ReadingType!
var dataSource: DataSource!
var readings: [T]
init(readingType: ReadingType, dataSource: DataSource, readings: [Reading]) {
self.readingType = readingType
self.dataSource = dataSource
self.readings = readings //produces error "Cannot assign value of type '[Reading]' to type '[_]'"
}
}
I am not quite sure why this is. I need the reading property to be generic since it can hold different types of readings that are subclassed from the Reading class. I'm new to swift generics and would like some help on how to properly achieve this
You need to declare the readings params as [T].
class GeneralUpdate<T: Reading> {
var readingType: ReadingType
var dataSource: DataSource
var readings: [T]
init(readingType: ReadingType, dataSource: DataSource, readings: [T]) {
self.readingType = readingType
self.dataSource = dataSource
self.readings = readings
}
}
And please get rid of all that ugly ! Implicitly Unwrapped Optionals.
Did you try to write something like:
init(readingType: ReadingType, dataSource: DataSource, readings: [T])
Please let me know if this help!

Protocol static var used in method

I have this swift code :
protocol Table {
static var tableName: String { get }
}
class User: Table {
internal static var tableName = "user"
}
I know would like to construct methods with Table protocol parameters.
Something like :
func doSomethingFrom(table: Table) {
print(table.tableName)
}
doSomethingFrom(table: User) // prints "user"
Is there a way to achieve this simply ?
This is the syntax you're looking for. You need to pass the type itself by appending .self. This is to prevent mistakes (since talking about types directly is kind of rare, but easy to do by accident). And you need to take a parameter of the type itself rather than an instance of that type.
func doSomethingFrom(table: Table.Type) {
print(table.tableName)
}
doSomethingFrom(table: User.self) // prints "user"
Since the value of tableName will be the same for all instances of User (because it is static) you can do:
func doSomethingFrom(table: Table) {
print(User.tableName)
// OR:
print(type(of: table).tableName)
}
Make sure though this is what you really want.

"Checkbox"/option type MongoDB schema design

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.