Can a Cerberus schema have an arbitrary name for the base dict? - cerberus

I need to validate Python dicts that will have arbitrary names. When I attempt to validate them using Cerberus, I get unknown field. Is there a way of allowing for arbitrary dict names?
I was thinking that keysrules might work, but it appears to only work on items within the base dict.
{'account_created': {'category': 'Accounts',
'conversion_event': True,
'description': 'A new account is created'}
}
I would like to be able to use an arbitrary name where account_created is in this dict.

Assuming you don't need to validate that base key, I just attempted to answer a question similar to this on the Cerberus GitHub. My suggestion was to maybe use a dynamically formed schema. You could follow the GitHub issue thread and see if anyone there comes up with a better answer.

Related

How to get away from Non-capitalized key value for Record construction?

I am using graphql, nexus-plugin-prisma, prisma to build a backend application using ReScript. The problem I face is that there are some columns starting with capital letter, and I want to set types for such schemas using records instead of objects. (to make use of pattern matching utility)
But ReScript prevents capitalized letters to appear as the very first character of a key of a record. Is there any way that I can somehow get away with this issue? Any help would be appreciated.
Usually when dealing with graphQL, the best way to circumvent this issue is to make use of graphQL aliasing feature:
fragment Foo on foo {
uncapitalizedAlias: CapitalizedName
}
edit: I don't know if the records you're trying to use are defined beforehand or generated by your GraphQL client, but you have other more general solutions when you want to bind to JS objects with capitalized names:
you can make use bs.as to change the name of the field (works both in Ocaml/Reason and Rescript syntaxes):
type myGQLrecord = {
#bs.as("CapitalizedName")
uncapitalizedName: string,
}
or you can directly use any identifier name you want for your field thanks to this feature of rescript syntax (works also for value identifiers):
type myGQLrecord = {
\"CapitalizedName": string
}
In my opinion, it makes it a bit more cumbersome to use though.

How to strip/ignore unused attributes when saving a model object?

I am sending angular model objects to bookshelf to save, but it may carry extraneous attributes that aren't in the database. When I save, bookshelf will try to save all attributes and say it can't find these extra attributes.
What is the recommended way to handle this? I'm sure I can set out an array of whitelisted attributes, and strip the object manually, but is there another way?
IE, is there a built in way to ignore unused attributes? Or is there a way to query the DB to get the array of columns, then use that to strip my object?
You may use parse() in addition to an array of permitted attributes, like Ghost team did.
Mode = bookshelf.Model.extend({
permittedAttributes: [ 'field1', 'field2', 'field3' ],
parse: function (attrs) {
return _.pick(attrs, this.permittedAttributes)
}
})
If you define parse() in a base model, all models that extend it will behave the same way
Tooting my own horn here, but I encountered this problem so many times that I created a plugin for bookshelf. Didn't want to have to manually define permitted attributes every single time.
https://www.npmjs.com/package/bookshelf-strip-save

Generating dictionary from a directory (for Lucene autocomplete)

Using Lucene 4.9 (Java), I've been looking for a way to implement an autocomplete/suggest feature. The goal is to use several of the fields data used in my indexed documents as the source of the dictionary. What is the best practice or suggested way of generating a dictionary based on this?
I tried LuceneDirectory, but the issue is that it only accepts one field, shown below:
LuceneDictionary ld = new LuceneDictionary(indexReader, "fieldname");
What i'm looking for is something similar to this, but with a possibility of being able to supply an array of string with fields to populate my dictionary.
My next step was to look at the source of the class of LuceneDirectory, hoping to make my own custom Dictionary class implementing the Lucene directory interface. This however, was outside of my scope, and I was hoping someone else might have already done an implementation of this, or know how I can proceed.
To summarize:
1: How do I create a dictionary from an existing directory, with data from multiple fields(terms)?
2: How do I keep the dictionary updated once it has been created? Should I regenerate it on a regular basis or are there any other best practices for this?
You can add multiple Dictionaries to a SpellChecker, like:
SpellChecker spellchecker = new SpellChecker(spellIndexDirectory);
spellchecker.indexDictionary(new LuceneDictionary(indexReader, "fieldname"));
spellchecker.indexDictionary(new LuceneDictionary(indexReader, "anotherfield"));

Map -spec in erlang

I'm looking to use the new erlang maps in a project, but I want to make sure to properly -spec everything I do. So my question is, what's the syntax for this? Is there anything like there is for records where I can specify field types? Or do I just use map() (and is that even the correct type spec?)
Yes, map() is one way to specify a map type. Another is #{}, which like map() means a map of any size. You can also specify #{ Type => Type } with one or more Type => Type pairs, as described in the Erlang Types and Function Specifications page. Also, have a look at the can_pkt() type in the source code for the maps1 test in the Erlang source code, as that specifies a record-like map type.

CouchDB: accessing nested structutes in map function

I have a document based on a xml structure that I have stored in a CouchDB database.
Some of the keys contains namespaces and are on the form "namespace:key":
{"mykey": {"nested:key": "nested value"}}
In the map function, I want to emit the nested value as a key, but the colon inside the name makes it hard...
emit(doc.mykey.nested:key, doc) <-- will not work.
Does anyone know how this can be solved?
A hint that its all just JSON and JavaScript got me some new ideas for searching.
It may be that colon in json keys ain't valid, but I found a way. By looking at the doc object as an hash, I can access my value in the following manner:
Doc.mykey['nested:key']
It works - for now...
That's because Couch is a JSON based document DB, and doc.mykey.nested:key is not a valid JSON identifier. JSON identifiers must match JavaScripts identifiers, and : is not a valid identifier character.
So, the simple answer is: "No, this won't and can't work". You need to change your identifiers.
Actually, I should qualify that.
Couch can use pretty much ANYTHING for it's views et al, and, in theory, works with any payload. But out of the box, it's just JavaScript and JSON.

Resources