Mongoose array required in schema validation not working - mongodb

In my schema I need to have a propery that is an array that must be always not null and not undefined.
So I defined it required, but the validation is not working as I expected, because if I omit the property no error is throw.
In case of simple property (not an array) this work as I expected
const nodeSchema = new Schema({
address: { type: String, required: true },
outputs: { type: [String], required: true }
})

I think you can fix it adding a custom validator like this:
const nodeSchema = new Schema({
address: { type: String, required: true },
outputs: {
type: [String],
required: true,
validate: [(value) => value.length > 0, 'No outputs'],
}
})
I hope it helps you.

Arrays implicitly have a default value of [] (empty array).
This was the problem

Array implicitly have a default value of [] (empty array).
if you add default: undefined it will fix the issue.

Related

what is the meaning of !: in this entity description?

In another question people were using !: in an entiy description.
what is the meaning of !: ?
example:
#Column({
type: 'text',
unique: true,
nullable: true,
})
resetPasswordToken!: string;
i think ?: means its an optional parameter, but what is the meaning of !: ?

How to create new Mongo ObjectId using variable?

Can we add a variable inside ObjectId instead of string?
For example:
const _id = ObjectId(variable);
or:
let variable = '5defabcty1234ds';
{ '$match': { _id: mongoose.Types.ObjectId(variable) } },
Yes, it will create the ObjectID from the string, see the documentation:
https://docs.mongodb.com/manual/reference/method/ObjectId/#specify-a-hexadecimal-string
Edit: The variable will just be replaced to its value (the string) during the execution, so still yes.

How do I combine 'nil' and an !Included datatype?

I am defining a RAML 1.0 datatype with property that can be 'nil.' The type of that property is an !included datatype which, whilst not giving any errors in the datatype definition causes the root object to throw a "Unresolved reference '!includereference-data.raml' at libraries/types/personal-details.raml (10, 12)'
I've tried converting the DataType to a library to try and implement a "uses" or "types" but that impacts all the other objects that include this data type.
#%RAML 1.0 DataType
properties:
DateOfBirth: datetime
FirstName: string
FamilyName: nil | string
PreferredName: nil | string
PreviousNames: nil | string
Title: !include reference-data.raml
Gender: nil | !include reference-data.raml
The 'Title' property is working as expected, the error is thrown against the Gender property - I actually want both to be nillable.
The datatype to hold nillable properties here is "name_block.raml"
#%RAML 1.0 DataType
uses:
lib: base_types.raml
type: object
properties:
FirstName: lib.string30
MiddleName: lib.nstring30
LastName: lib.string150
The included library (base_types.raml) is defined as
#%RAML 1.0 Library
types:
string30:
type: string
minLength: 0
maxLength: 30
string150:
type: string
minLength: 0
maxLength: 150
nstring30:
type: string | nil
If anyone knows of a better way - It would be great to see it.

Swift cannot create array of tuples containing a nested type

I'm trying to do something that should be possible, but I'm getting a strange error. I have a nested type Gravl.Node and am trying to instantiate an array of tuples like so:
var attributes = [(attribute: String?, value: Gravl.Node)]()
…but Swift is complaining:
Cannot call value of non-function type '[(attribute: String?.Type, value: Gravl.Node.Type)]'
Any idea what gives? If I replace the nested type with something else it works fine.
Is this a bug in the parser/compiler?
Yes, this is a bug as noted by this question.
Following the example of that Q & A, you can change the syntax of the way you create the array:
var attributes: [(attribute: String?, value: Gravl.Node)] = []
or
var attributes = Array<(attribute: String?, value: Gravl.Node)>()
In this case, you can also work around the issue by creating a typealias:
Example:
class Gravl {
enum Node {
case first, last
}
}
typealias Gravl_Node = Gravl.Node // work around Swift bug
var attributes = [(attribute: String?, value: Gravl_Node)]()
attributes.append((attribute: "hello", value: Gravl.Node.first))

How do I query for nil values with CloudKit.js?

Using CloudKit.js, how do I construct a query that matches items where a field is nil? Every permutation I've tried fails - either it's clearly matching on a string value (i.e. "null" or "nil") or it throws an error if I actually try to pass 'null'.
Any ideas? None of the below work.
filterBy: [{
fieldName: 'customerNumber',
comparator: 'EQUALS',
fieldValue: { value: "nil" }
}]
filterBy: [{
fieldName: 'customerNumber',
comparator: 'EQUALS',
fieldValue: { value: null }
}]
filterBy: [{
fieldName: 'customerNumber',
comparator: 'EQUALS',
fieldValue: { value: "null" }
}]
filterBy: [{
fieldName: 'customerNumber',
comparator: 'EQUALS',
fieldValue: { value: "" }
}]
I don't think there is any option to query for nil values in CloudKit. Also not from native code. I have tried a lot of predicates, but non seems to work.
You do have to be aware that CloudKit is a key-value store. When a value is nil, then the key won't be there in the record. There is no option to query for non existing keys.
The closest I have come to a good solution was with a predicate with the format "NOT myField => ''" sorry, don't know the format for .js
In the CloudKit CKQuery documentation there is nothing mentioned about nil fields.
In the documentation for NSPredicates there are sample's for nil values.
But it was already clear that CloudKit has implemented only a subset of the possibilities of NSPredicate (see CKQuery class reference)
I think the best way to handle this is by not using/depending on nil values. If you do need to check some nil status, then instead you could add an extra field that would represent that status.