I have the following JSON structure:
{
key1: "value1",
key2: "value2",
transactions: [
{
receiverId: '12341',
senderId: '51634',
someOtherKey: 'value'
},
{
receiverId: '97561',
senderId: '46510',
someOtherKey: 'value'
}
]
}
I'm trying to write some Joi code to validate that each object in the transactions array is unique i.e. a combination of receiverId and senderId is only ever present once. There can be a variable number of elements in the transactions array but there will always be at least 1.
Any thoughts?
You can use array.unique
const array_uniq_schema = Joi.array().unique((a, b) => a.receiverId === b.receiverId && a.senderId === b.senderId);
So for the whole object the schema would be (assuming all properties are required):
const schema = Joi.object({
key1: Joi.string().required(),
key2: Joi.string().required(),
transactions: array_uniq_schema.required(),
});
an easy way :
const schema = Joi.object({
transactions: Joi.array()
.unique('receiverId')
.unique('senderId')
.required(),
});
This way it returns an error for each field (one error for ReceivedId and another one for senderId)
Related
i have a JavaScript map with multiple values it is like this (i simplified the code for obvious reasons):
Map(2) {
'group1' => {
username: 'userTest',
dsId: '710300636817653790',
openDate: '2021-12-13 18:29:16'
},
'group2' => {
username: 'Juojo',
dsId: '477581625841156106',
openDate: '2021-12-13 18:29:23'
}
}
I want to get the username value (Juojo) of the second group of data. I tried doing this:
console.log(map.get(group2.username));
but this logs me "undefined", when i try without the ".username" (console.log(map.get(group2));) it replys me:
{
username: 'Juojo',
dsId: '477581625841156106',
openDate: '2021-12-13 18:29:23'
}
I only want the reply to be "Juojo"
Access the username property on the object returned by get.
console.log(map.get(group2).username);
If it is possible for the key to not exist, you can use the optional chaining operator, which will produce undefined in that case instead of causing an error.
console.log(map.get(group2)?.username);
You should try to get the object which has stored in your map then try to read the property of returned object, like below example:
const myMap = new Map([
['group1',{
username: 'userTest',
dsId: '710300636817653790',
openDate: '2021-12-13 18:29:16'
}],
['group2', {
username: 'Juojo',
dsId: '477581625841156106',
openDate: '2021-12-13 18:29:23'
}]
])
const userName = myMap.get('group2').username;
console.log(userName);
I have this code:
let peopleInRoom = [];
for (let message of messages) {
for (let email of message.user.email) {
if (!peopleInRoom.includes(email)) {
peopleInRoom.push(email);
}
}
}
let peopleInRoomElement = peopleInRoom.map(person => (
<li>{person}</li>
))
Basically I am trying to get all the unique emails and display them.
Is there a shorter and more efficient way (maybe some ES6 features) to write the same code? Seems too much code than needed.
I looked at this answer: How to get distinct values from an array of objects in JavaScript?
EDIT: Above code does not do what I want.
My data looks like this:
[
{
text: 'foo',
user: { email: 'foo#bar.com', password: 'foo' }
},
{
text: 'baz',
user: { email: 'baz#qux.com', password: 'baz' }
}
]
The objects are all messages. And I want to get an array of all the unique emails from each message
You can use the Set object that is built into JavaScript. Set object actually keep the distinct primitive values.
const messages = [
{
text: 'foo',
user: { email: 'foo#bar.com', password: 'foo' }
},
{
text: 'baz',
user: { email: 'baz#qux.com', password: 'baz' }
}
]
const peopleInRoom = [...new Set(messages.map(message => message.user.email))];
It actually extracts the email from each message and then passes it to the Set object which only keeps the unique set of emails. After that, it will spread that Set to the array, since Set is also an iterable and returns the array of the people in room.
If I understand correctly, people have messages, messages have email addresses and the OP seeks the unique set of email addresses. If that's all the data available, then there's no alternative but to iterate it, checking to see if each email has been collected already, and collecting it if it hasn't been.
There are ways to conceal this work by doing it in library code. Probably the highest level utility is lodash's _.uniqueBy, but the work must be done one way or another.
The Set object enforces uniqueness of its elements. You can use it this way:
const peopleInRoom = Array.from(new Set(messages.map(message => message.user.email)));
First you can make an array of all the email addresses:
const data = [
{
text: 'foo',
user: { email: 'foo#bar.com', password: 'foo' }
},
{
text: 'baz',
user: { email: 'baz#qux.com', password: 'baz' }
}
]
const emailArray = data.map((elem) => {
return elem.user.email;
}
and then you can filter them to be unique:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
emailArrayFiltered = emailArray.filter(onlyUnique);
see here for ref link
What about:
const peopleInRoom = messages.map(e => e.user.email && e.user.email);
console.log(peopleInRoom)
gives you this output:
["foo#bar.com", "baz#qux.com"]
Joi array required validation not working. I have an array userData which contain objects, keys dateMilli and value. I put required everywhere But if I pass an empty array of userData []. It is not throwing any error.
Joi Version is "#hapi/joi": "^16.1.8", struggling with Joi docs but nothing works.
userId is "123" and userData is []
const data = { userId, userData };
const schema = Joi.object({
userId: Joi.string().required(),
userData: Joi.array().items({
dateMilli: Joi.number().required(),
value: Joi.string().valid("YES", "NO").required()
}).required(),
});
let validate = schema.validate(data);
if (!validate || validate.hasOwnProperty("error")) {
return res.send("Invalid parameters");
}
You need to use .min with array to disallow empty arrays.
try this
const data = {"userId": "123", "userData": []};
const Joi = require("#hapi/joi");
const schema = Joi.object({
"userId": Joi.string().required(),
"userData": Joi.array().items({
"dateMilli": Joi.number().required(),
"value": Joi.string().valid("YES", "NO").required()
}).min(1).required()
});
const validate = schema.validate(data);
console.log(validate);
I have object which I want to validate.
// valid object because all values of keys are present in details object
var object = {
details: {
key1: 'stringValue1',
key2: 'stringValue2',
key3: 'stringValue3'
},
keys: ['key1', 'key2', 'key3']
}
// invalid object as key5 is not present in details
var object = {
details: {
key4: 'stringValue4'
},
keys: ['key4', 'key5']
}
// invalid object as key5 is not present and key8 should not exist in details
var object = {
details: {
key4: 'stringValue4',
key8: 'stringValue8',
},
keys: ['key4', 'key5']
}
All the keys present in keys should be present in details also.
I tried this using Joi.ref()
var schema = Joi.object({
details: Joi.object().keys(Object.assign({}, ...Object.entries({...Joi.ref('keys')}).map(([a,b]) => ({ [b]: Joi.string() })))),
keys: Joi.array()
})
But this is not working because Joi.ref('keys') will get resolved at validation time.
How can I validate this object using Joi?
Using object.pattern and array.length
var schema = Joi.object({
details: Joi.object().pattern(Joi.in('keys'), Joi.string()),
keys: Joi.array().length(Joi.ref('details', {
adjust: (value) => Object.keys(value).length
}))
});
stackblitz
You can validate the array(if you want) then make a dynamic schema and validate that.
const arrSchema = Joi.object({
keys: Joi.array()
});
then,
const newSchema = Joi.object({
details: Joi.object().keys(data.keys.reduce((p, k) => {
p[k] = Joi.string().required();
return p;
},{})),
keys: Joi.array()
})
This should probably do it.
You have to set allowUnknown: true in validate() option.
Can spread operator solve below problem? Imagine I have more fields, then I have to declare req.body.something for every single fields, that's so tedious.
app.use((res,req,next) => {
const obj = {
name: req.body.name,
age: req.body.age,
gender: req.body.gender
}
//
User.saveUser(resp => res.json(resp)) //User model
})
You can use destructuring assignment:
const obj = req.body;
const { name, age, gender } = obj;
But, still you will have to validate it, and count all of them in your scheme.
Update:
Adding some validation example.
Assuming such schema in your route:
const tv4 = require('tv4');
const schema = {
type: 'object',
properties: {
name: 'string',
age: number,
gender: {
type: 'string',
pattern: /f|m/i
}
},
required: ['name']
};
And then, in your handler you validate:
if (tv4.validate(req.body, schema) {
// continue your logic here
} else {
// return 400 status here
}
You can use lodash's pick():
_.pick(object, [paths])
Creates an object composed of the picked object properties.
Example code is:
const _ = require('lodash');
...
const obj = _.pick(req.body, ['name', 'age', 'gender']);
If gender does not exist in req.body, it would be ignored -- the result obj object won't have a gender field.
If all the req.body fields are needed, you can just assign req.body to obj:
const obj = req.body;
To validate req.body content, you can use lodash's .has():
_.has(object, path)
Checks if path is a direct property of object.
Example code would be:
_.has(req.body, ['name', 'age', 'gender']); // return true if all fields exist.
You can use destructuring assignment
const { name, age, gender } = req.body
or if you wanna use spread operation, you can use :
const obj = { ...req.body }
Hope it helps!