How to add id field to JsonObject using SORM in play framework on scala? - sorm

this is my case class:
case class Admin(name: String, surname: String, login: String, password: String
this is my json formatter:
implicit val adminWrite: Writes[Admin] = (
(JsPath \ "name").write[String] and
(JsPath \ "surname").write[String] and
(JsPath \ "login").write[String] and
(JsPath \ "password").write[String]
)(unlift(Admin.unapply))
what i get:
{
name: "ddd",
surname: "ddd",
login: "djamik123",
password: "djamik123"
}
what i need:
{
id: 1,
name: "ddd",
surname: "ddd",
login: "djamik123",
password: "djamik123"
}
Is there a way to solve this problem?

Related

what would be the correct type for "tools" in EnterpriseInput input?

input EnterpriseInput {
cnpj: String
name: String
email: String
password: String
tools: Tool
}
type Enterprise {
id: ID!
cnpj: String!
name: String!
email: String!
password: String!
tools: [Tool]
}
type Tool {
id: ID!
name: String!
brand: String!
cod: String!
qnt: Int!
}
type Query {
enterprises: [Enterprise]
enterprise( id: ID!): Enterprise
}
type Mutation {
createEnterprise( input: EnterpriseInput!): Enterprise!
# updateEnterprise( id: ID!, input: EnterpriseInput!): Enterprise!
deleteEnterprise( id: ID!): Boolean,
}
Error message on console:
C:\Users\Elias\code\web\backEnd-gestordeferramentas\node_modules\graphql\type\validate.js:69
throw new Error(errors.map(function (error) {
^
Error: The type of EnterpriseInput.tools must be Input Type but got: Tool.
at assertValidSchema (C:\Users\Elias\code\web\backEnd-…
In a mutation you cannot use standard Object type and must use Input type instead. And in input type you must use input type
So instead of
input EnterpriseInput {
cnpj: String
name: String
email: String
password: String
tools: Tool
}
Try this
input ToolInput {
id: ID!
name: String!
brand: String!
cod: String!
qnt: Int!
}
input EnterpriseInput {
cnpj: String
name: String
email: String
password: String
tools: ToolInput
}

Karate match each on response assertion is failing to identify missing keys in response

I have a match each assertion like below in my code. Just tried creating similar examples as my code, just to explain the issue.
Scenario: Example scenario 1
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123"
},
{
id: 2,
name: "David",
password: "abc123"
},
{
id: 3,
name: "Mike",
password: "abc123"
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
Test status : Pass
Password field is missing in object 4(where id=4). Above test is passing for me. I am expecting Karate to fail the test in this case. How can I make my test fail in this case?
Scenario: Example scenario 2
* def response =
"""
[
{
id: 1,
name: "John",
},
{
id: 2,
name: "David",
},
{
id: 3,
name: "Mike",
},
{
id: 4,
name: "Johny"
}
]
"""
* match each response[*].password contains 'abc123'
Test status : Pass
Here, there is no password field at all in response. But my test is passing.
Need a work around to fail these kind of scenarios.
Example 3 :
* def response =
"""
[
{
id: 1,
name: "John",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 2,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
},
{
id: 3,
name: "David",
password: "abc123",
skills :[ "training", "coding"
]
},
{
id: 4,
name: "David",
password: "abc123",
skills :[ "training", "management"
]
}
]
"""
Considering * match each response contains { password: 'abc123' } format(mentioned by #peter) to check example 1 and 2, what if I want to check skills array having 'training' in each object under response? How can I achieve this?
you can use match each to validate the json schema
https://github.com/intuit/karate#match-each
Note that response[*].password is a JsonPath expression that will return an array of all the password key-values found and will return only 3 in your case.
What you are looking for is this:
* match each response contains { password: 'abc123' }

Vue.js - using Vuelidate url domain should match email domain

I have two inputs field : url and email
I'm using Vuelidate and I want to make a custom validator where it checks if the url domain name is equal to the email domain name.
Valid example :
url: (https://)www.mydomain.example
email: johndoe#mydomain.example
Invalid example :
url: (https://)www.mydomain.example
email: johndoe#somedomaine.example
validations object :
validations: {
form: {
url: { required, url, maxLength: maxLength(2083) },
email: { required, email, maxLength: maxLength(255) },
lastName: { required, maxLength: maxLength(100) },
firstName: { required, maxLength: maxLength(100) }
}
},
You could use a custom validator called matchUrl as follow :
const matchUrl=(value, vm) =>
value.substring(value.indexOf("#")+1) ===vm.url.substring(vm.url.indexOf(".")+1);
And use it as :
validations: {
form: {
url: { required, url, maxLength: maxLength(2083) },
email: { required,matchUrl, email, maxLength: maxLength(255) },
lastName: { required, maxLength: maxLength(100) },
firstName: { required, maxLength: maxLength(100) }
}
},
check this working example

Creating mongoose schema that contains array of Objects

How do I create a mongoose schema that has the following structure
{
data: {
name: "John doe",
attributes: [
{
text: "Sample text",
created_at: "2018-08-23"
},
{
text: "Sample text 2",
created_at: "2018-08-23"
}
],
created_at: "2018-08-23"
}
}
This can simply be done with Arrays of Objects instead of creating New Schemas. I don't know if there might be some implications for the optimization.
attributes: [{
text: String,
created_at: Date
}],
This is following the official Mongoose documentation.
You can try this
const sampleSchema = new mongoose.Schema({
data: {
type: dataSchema
}
});
const dataSchema = new mongoose.Schema({
name: String,
attributes: [attributeSchema],
created_at: Date
});
const attributeSchema = new mongoose.Schema({
text: String,
created_at: Date
});

Create index on multikey and nested document using mongoose

I'm creating index using mongoose it will check uniqueness of Name, PName and OName (Name+PName+OName should be unique). Please check below implementation
var MySchema = new mongoose.Schema({
Name: { type: String, required: true},
Details: [{
PName: { type: String, required: true},
OName: { type: String, required: true}
}]
});
MySchema.index({Name: 1, Details.PName: 1, Details.OName:1 }, {unique: true});
Document
{"Name" : "Testing123","Details" : [{"PName" : "Page1", "OName" : "Ob1"},
{"PName" : "Page1", "OName" : "Ob1"}]}
I need to restrict above document for insertion as the Name, PName and OName is not unique.