-
Notifications
You must be signed in to change notification settings - Fork 1
Option for giving default values in Schema #1
Comments
Also how can I deal with nested documents? |
I'll add that in the next version for sure. Good idea! As for nested Documents let subschema: Schema = [
"name": (match: .number, required: true)
]
let mySchema: Schema = [
"subdocument": (match: .object(subschema), required: true)
] |
Mainecoon build failing with new MongoKitten version
|
I do get some warnings but I don't see where it would crash. What line and file is this in? |
Still a couple of more errors in the same files |
Can you try to clean and re-fetch the dependencies? I think this is caused by a bad tag I placed a few days ago on BSON and MongoKitten. |
OK I cleaned the project and it is working. I checked that MongoKitten version had been changed from 2.0.7 to 2.0.8. So that was the main problem |
Also I want the nested documents to be validated. So do I have to write down validate function for each and every nested document? let subschema: Schema = [
"name": (match: .number, required: true)
]
let mySchema: Schema = [
"subdocument1": (match: .object(subschema), required: true),
"subdocument2": (match: .object(subschema), required: true),
"subdocument3": (match: .object(subschema), required: true),
]
subschema.validate(bsonDocument["subdocument1"])
subschema.validate(bsonDocument["subdocument2"])
subschema.validate(bsonDocument["subdocument3"])
mySchema.validate(bsonDocument) Or will it just work with let mySchema: Schema = [
"subdocument1": (match: .object(subschema), required: true),
"subdocument2": (match: .object(subschema), required: true),
"subdocument3": (match: .object(subschema), required: true),
] |
I'd use the |
I have values in the sub document which cannot be null. So I want to validate those sub values also. How can you achieve that? |
Ok it is working my bad. I wanted to ask that I have a document like this subdocument: Document =[
"id": 1,
"subdocument": [
["name": "Hello"],
["name": "World"]
]
] Subdocument Schema subSchema: Schema = [
"name": (match: .string, required: true)
] Maindocument Schema mainSchema: Schema = [
"id": (match: .number, required: true),
"subdocument": (match: .object(subschema), required: true)
] But the validation of subschema is failing |
Are you there to reply? |
Hey there. @sujaykakkad |
Hey @Joannis |
Sorry about the late reply. I was looking for an eternity but I just re-read your code and it seems like a pretty simple but hard-to-spot mistake. Didn't test it myself since I'm still setting up my working environment but: let mainSchema: Schema = [
"id": (match: .number, required: true),
"subdocument": (match: .array(subschema), required: true)
] Notice how your document example contains an array with two instances of a Document with a name each. |
OK so I have an array of schemas and not just a single schema. |
Thanks for the solution it is working as expected. |
How can i validate BSON values like object id or datetime let mainSchema: Schema = [
"id": (match: .objectId, required: true),
"date_created": (match: .dateTime, required: true)
] |
Date has the matcher |
Ok so I updated the version to 0.2.1 {
"expiry_date": {
"$date": "2016-12-25T16:07:23+05:30"
}
} This is the raw string then I use let myDoc = try Document(extendedJSON: thisString);
print(myDoc["expiry_date"]) //This prints dateTime(2016-12-25 11:37:23 +0000)
mySchema: Schema = [
"expiry_date": (match: .date, require: true)
]
mySchema.validate(BSONDocument(myDoc)) //This gives expiry_date does not match expeced type test |
It should work now. Forgot to add a type test for DateTime. |
@sujaykakkad I'm working on the "next version" now. How do you prefer to see the default value being implemented? |
The default value should be implemented on validate function where it will return a document .valid(document) |
Also what version should I write in my package.swift file for the latest Mainecoon version. Also can you tell me how can I implement regex validation? |
Also AllowExtraKeys is not working |
If you locked on let regex: NSRegularExpression = ...
let schema: Schema = [
"key": (match: .matchingRegex(regex), required: true)
] |
Whoops. Something went wrong there with pushing. Sorry about that! If you update your dependency on Mainecoon now you'll get 0.2.2 properly. And about allowExtraKeys, I'm looking into it. I'll notify you of a 0.2.3 whenever I fixed it 👍 |
@sujaykakkad I've fixed the problem and added a test. I'm not sure how it'll function in more complex scenario's yet. I'll work towards that now |
0.2.3 version has this function case matchingRegex(String, withOptions: String) How can I use this? |
The first string is the regex pattern, the options are a MongoDB options string which are sent to MongoDB only. |
Oh, by the way, thanks for all the feedback! It's really helping me a lot! |
Thanks for helping me out with my validations. Also Why don't you try validations similar to those given in http://json-schema.org/. let mySchema: Schema = [
"type": "object",
"properties": [
"id": ["type": "number"],
"name": ["type": "string"],
"address":[
"type": "object",
"properties":[
"city": ["type": "string"],
"state": ["type": "string"],
"zip": ["type": "number"]
],
"required": ["city", "zip"]]
]
"required": ["name", "address"]
]
] |
I could look into supporting this! |
Hey I had an issue the allow extra keys feature is not working for nested json array let innerSchema = [
"first": (match: .string, required: true),
"last": (match: .string, required: true),
]
let outerSchema = [
"id": (match: .number, required: true),
"name": (match: .array(of: .object(matching: innerSchema)), required: true),
]
let document: Document = [
"id": 1,
"name": [
["first": "Hello", "last": "World"],
["first": "JSON", "last": "Schema", "extra": "Not Allowed"] //This is an extra key which shouldn't be allowed
]
]
outerSchema.validate(BSONDocument(document), allowExtraKeys: false){ |
Also I need help I have a document something like this let mydoc: Document =[
"key": [
"a" : "1",
"b": "2",
],
"dependent_key": [
"a_to_b": "Some random string",
"b_to_a": "Some other random string"
]
] There are different cases let mydoc: Document =[
"key": [
"a" : "1",
"b": "2",
"c": "3",
],
"dependent_key": [
"a_to_b": "Some random string",
"b_to_a": "Some random string",
"a_to_c": "Some random string",
"c_to_a": "Some random string",
"b_to_c": "Some random string",
"c_to_b": "Some random string",
]
] let mydoc: Document =[
"key": [
"a" : "1",
],
"dependent_key": []
] The dependent key cannot be generated by me but I have to validate that if "key" has x values then the dependent key should have 2*(x-1) values and that to only those specific keys and not more than that |
The text was updated successfully, but these errors were encountered: