Validation function to ensure unique field in couchdb - validation

Is it possible to write a validation function to ensure a field of a new document is unique?
Imagine I'm trying to write a validation function that does not allow for two users to have the same email. Every time I create a new user, the validation function will be called and will probably look something like this:
function (newDoc, oldDoc) {
//How do I get this array to contain the emails of all the users?
var allEmail;
if (allEmail.indexOf(newDoc.email) !== -1) {
throw "This email adress is already taken";
}
};
How can I fill the array allEmail to contain all emails of the users?
Is it possible to call views in the validation function?

Not possible. Validation function only operates with updated doc and his previous revision and it cannot access to other documents. The only field that guaranteed to be unique is document _id. If it's possible and doesn't produce security/privacy issues, use email as doc _id to be ensure that it's unique.
Otherwise you have to create a view with target field as a key and check for it existence first before create a new doc on the client side. However, this logic easily becomes ruined when you'll replicate docs from other instance.

If the application is in offline, the above suggested solution how will react. Local pouch view can check and return the pouch results alone. There may be a high chance of same value entered from some other end and updated to couch db.
Do you have workaround for this case ?

Related

servicenow getting value from extended table

Good Evening, I am sure this is a very simple question. But I am having no luck finding a correct answer.
I have onLoad client script on a request form. The form has a variable that I need to auto populate, the value is on a sys_user extended table.
I am not sure how to pull that value from the sys_user extended table.
I know to use a GlideRecord to get the current user on the sys_user, but after that is were I am having the issue.
Thanks for any help or suggestions you may have.
James
As you mentioned, GlideRecord should be what you need to use. The exact script will vary based off of what field you are trying to query on your extended table.
var gr = new GlideRecord("extended_table_name");
gr.addQuery("field_name", "query");
gr.query();
if (gr.next()) {//action you want to take}
You can add as many addQuery() methods as needed to build the query as specific as you require. The query() method then runs the query and return a new GlideRecord object. The .next() method checks to see if there is another record in the GlideRecord object and advances to the next record if so. After running the script above, you can access any properties on the GlideRecord you may need by simply dotwalking to them. i.e:
sys_id = gr.sys_id;
name = gr.getDisplayValue();

Set a field value directly from an asyncValidate result

I have a form where a user enters an Id into a text field, and I then I validate said Id on the server. If the Id does not exist in the system, I display an error. I do this using async validation. If the Id does exist, however, the server will return a record from our database. I want to use those values to auto populate other fields in the form.
How would I accomplish this?
I did some searching and the closest solution I found was this other question on StackOverflow. The thing is, I want to change the value after my asycValidate logic has succeeded. I don't think I can trigger the action creator from inside asyncValidate, and I'm not aware of a way to have asyncValidate trigger a callback from inside the form component.
I was able to get it to work, by following the solutions discussed in the following thread: https://github.com/erikras/redux-form/issues/442

Relational data query in Parse - retrieve users with and without messages

I have a relation on user in Parse 'messages'
I want to issue a query via cloudcoud that retrieves all friends of user (another relation, friendship) AND messages if there are any messages where the current user is the recipient.
I got this far:
var relation = current.relation("friendship");
var queryRel = relation.query();
queryRel.select("username","color","count");
queryRel.ascending("username");
var innerQuery = new Parse.Query("Message");
innerQuery.equalTo("recipient",current);
queryRel.matchesQuery("messages", innerQuery);
...but that only retrieves friends of the current user who have messages, not those who are friends but don't have messages. I need both!
If at all possible, I also want to retrieve the messages themselves, not just pointers. This is all so I can return properly organised data to the client so my apps don't have to do lots of looping to sort things.
I would use the include method here. The way it works is you would do queryRel.include("message"); so you won't just get the pointers. I believe you should also remove the second query for this to work.
The above code is assuming your column pointing to the Message table is called "message"

How to prevent user from modifying some fields on the form?

I am using MVC3 and EF4 to write a web application. I am using an action header like below to capture the form values submitted by the user.
<HttpPost()>
Public Function Edit(ByVal prod as Product) As ActionResult
I use the below code for updating the record.
db.Attach(prod)
db.ObjectStateManager.ChangeObjectState(prod, EntityState.Modified)
db.SaveChanges()
I get the submitted values in prod object which I update in the database. The problem is that there are some users who are not allowed to modify certain fields in a Product, say ProductCost. I have disabled the textboxes for such fields in the HTML. But since it is clientside, the user can easily enable it using some tool like Firebug and modify the value.
The only solution I could come up was to retrieve the existing record from the database and copy its ProductCost value into prod.ProductCost. But I don't like firing a query for achieving this. Is there a better way to achieve this?
Edit: I found the below link to update particular fields. How to update only one field using Entity Framework?
You can use the below code to modify a particular field.
context.ObjectStateManager.GetObjectStateEntry(user).SetModifiedProperty("FieldName");
Now the question is do I have to write the above statement for every field the user is able to modify? If yes, suppose the Product model has 10 fields (1 primary key) and the user is allowed to modify all of them except the primary key, I need to write 9 statements?? Is there a method where you can specify multiple properties at once. Or even better something where you specify the properties which are not modified. (Note: I know I can run a loop over an array of field names to avoid writing 9 statements. I am asking for an alternative method and not refactoring the above)
Never trust client data. Always have your server code to validate the input and do appropriate actions.
I would create separate overloads of my Respiratory method update the product in different ways and then check what is the current user's access type, If he is admin, i will call the overload which updates everything, if he is a manager, i will call the method which updates name,imageUrl and price and if he is an employee, i will call the method which updates only name and ImageURL
[HttpPost]
public ActionResult Edit(Product prod)
{
if(ModelState.IsValid)
{
string userType=GetCurrentUserTypeFromSomeWhere();
if(userType=="admin")
{
repo.UpdateProduct(prod);
}
else if(userType=="manager")
{
repo.UpdateProduct(prod.ID, prod.Name, prod.ImageUrl, prod.Price);
}
else if(userType=="employee")
{
repo.UpdateProduct(prod.ID, prod.Name, prod.ImageUrl);
}
return RedirectToAction("Updated",new {id=prod.ID});
}
}

Saving table relationships with Datamapper ORM

I am new to Object Model Mapping and just read the documentation of the CodeIgniter DataMapper. I have tried the examples and I am quite impressed in how little time one can achive a lot.
But I don't seem to get one problem solved:
Lets say I have two tables: customer and address. A customer can have many addresses but am address only one customer.
Now, I have a registration form where a new customer has to enter his personal data which is stored in the customer table and his address which is stored in the address table.
Validation is done in the specific models. My problem is how can I ensure that the data is valid for both tables prior to saving them to database.
At the moment it is possible that the customer enters the correct data for the customer table which validates and gets saved to db but the address data does not validate and is not saved.
In short: I only want to save the data of any table to db if the data for all tables validates else I want to get the error messages.
Thanks for any help in advance.
First way is to use transactions, second - validate both address and customer data, and if both is valid - store.
It's not clear from the documentation, but you can call the validate() method prior to calling save():
public function test()
{
// Customers require an email address
$cust = new Customer();
$cust->first_name = "Me";
$cust->validate();
if ($cust->error->string)
{
echo $cust->error->string;
// outputs The Email Address field is required.
}
else
{
// validation passed, save customer and related address to db
$cust->save(array($address));
}
}
If you have validation rules defined, you don't need to explicitly call validate(), the validation rules will also be checked when you call save().
In case validation failed, save() will return FALSE, after which you can check $this->valid to see if a validation error caused the save to fail.

Resources