Skip to content
This repository has been archived by the owner on Jul 31, 2022. It is now read-only.

Option for giving default values in Schema #1

Open
sujaykakkad opened this issue Nov 11, 2016 · 35 comments
Open

Option for giving default values in Schema #1

sujaykakkad opened this issue Nov 11, 2016 · 35 comments

Comments

@sujaykakkad
Copy link

let mySchema: Schema = [
    "balance" = (match: .number, required: false, defaultValue: 0.0) //Something like this
]
@sujaykakkad
Copy link
Author

Also how can I deal with nested documents?

@Joannis
Copy link
Member

Joannis commented Nov 11, 2016

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)
]

@sujaykakkad
Copy link
Author

Mainecoon build failing with new MongoKitten version

error: use of undeclared type 'BSONDocument'
    public init?(_ document: BSONDocument, inDatabase db: Database) {

@Joannis
Copy link
Member

Joannis commented Nov 11, 2016

I do get some warnings but I don't see where it would crash. What line and file is this in?

@sujaykakkad
Copy link
Author

Mainecoon-0.1.0/Sources/EmbeddedReference.swift:9:28: error: use of undeclared type 'BSONDocument'
Mainecoon-0.1.0/Sources/Instance.swift:50:22: error: use of undeclared type 'BSONDocument'
    init(_ document: BSONDocument, projectedBy projection: Projection, validatingDocument validate: Bool) throws
Mainecoon-0.1.0/Sources/Instance+Operations.swift:75:30: error: use of unresolved identifier 'BSONDocument'
        return try Self.init(BSONDocument(document), projectedBy: projection, validatingDocument: true)
Mainecoon-0.1.0/Sources/Schema.swift:181:37: error: contextual type 'ValueConvertible' cannot be used with array literal
                    return ["$and": [

Still a couple of more errors in the same files

@Joannis
Copy link
Member

Joannis commented Nov 14, 2016

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.

@sujaykakkad
Copy link
Author

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

@sujaykakkad
Copy link
Author

Also I want the nested documents to be validated. So do I have to write down validate function for each and every nested document?
eg

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),
]

@Joannis
Copy link
Member

Joannis commented Nov 14, 2016

I'd use the mySchema to validate bsonDocument itself rather than the subdocuments

@sujaykakkad
Copy link
Author

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?

@sujaykakkad
Copy link
Author

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

@sujaykakkad
Copy link
Author

Are you there to reply?

@Joannis
Copy link
Member

Joannis commented Nov 23, 2016

Hey there. @sujaykakkad
I was without laptop for a while so couldn't properly respond. I'm looking into it now.

@sujaykakkad
Copy link
Author

Hey @Joannis
So did you find the solution?

@Joannis
Copy link
Member

Joannis commented Nov 24, 2016

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.
Because it's an array you'll have to use the array enum case in the schema.

@sujaykakkad
Copy link
Author

OK so I have an array of schemas and not just a single schema.

@sujaykakkad
Copy link
Author

Thanks for the solution it is working as expected.

@sujaykakkad
Copy link
Author

How can i validate BSON values like object id or datetime
Something like

let mainSchema: Schema = [
    "id": (match: .objectId, required: true),
    "date_created": (match: .dateTime, required: true)    
]

@Joannis
Copy link
Member

Joannis commented Nov 28, 2016

Date has the matcher .date. ObjectId is .objectId. Which Mainecoon version do you run? Because it might not have been added in your version, so I could back port it.

@sujaykakkad
Copy link
Author

Ok so I updated the version to 0.2.1
But the date type is still giving errors

{
  "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 

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

It should work now. Forgot to add a type test for DateTime.

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

@sujaykakkad I'm working on the "next version" now. How do you prefer to see the default value being implemented?

@sujaykakkad
Copy link
Author

The default value should be implemented on validate function where it will return a document
If the value is invalid the validate function returns invalid enum with message so when the document is valid the .valid enum should return a document

.valid(document)

@sujaykakkad
Copy link
Author

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?

@sujaykakkad
Copy link
Author

Also AllowExtraKeys is not working

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

If you locked on majorVersion: 0, minor: 2 you'll only need to update your dependencies using swift package update. Otherwise the tag is "0.2.3". Regex validation is done using:

let regex: NSRegularExpression = ...

let schema: Schema = [
  "key": (match: .matchingRegex(regex), required: true)
]

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

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 👍

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

@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

@sujaykakkad
Copy link
Author

0.2.3 version has this function

case matchingRegex(String, withOptions: String)

How can I use this?

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

The first string is the regex pattern, the options are a MongoDB options string which are sent to MongoDB only.

@Joannis
Copy link
Member

Joannis commented Nov 29, 2016

Oh, by the way, thanks for all the feedback! It's really helping me a lot!

@sujaykakkad
Copy link
Author

Thanks for helping me out with my validations. Also Why don't you try validations similar to those given in http://json-schema.org/.
eg

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"]
    ]
]

@Joannis
Copy link
Member

Joannis commented Dec 1, 2016

I could look into supporting this!

@sujaykakkad
Copy link
Author

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){

@sujaykakkad
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants