Validate email with JOI Library - node.js

I already use JOI to validate many different things. Now I need to validate an email as well.
Validation logic looks like this:
const emailSchema = Joi.string().email({ minDomainSegments: 2 }).lowercase().required();
const resource = Joi.validate(email, emailSchema);
if (resource.error !== null) {
return true;
}
The thing is it fails to validate the following format:
const email = 6AEFE056-485C-42C0-9059-AF4D614C8FBE#anon
It sees it as a regular email, but it is not.
It does work with regular emails.
What am I doing wrong? I would really want to use JOI for this.

Joi version that I have used is 14.3.1
And coincidentally that is the last version on NPM. Joi moved under Hapi
And while being with Hapi, they release several new versions. The way I was using a version was designed for a newer release, but not mine. Therefore, I used older version notation.
When I find time, I will also switch to newer version.
This is the sample that works with older version.
const emailSchema = Joi.string().email({ minDomainAtoms: 2 });

Related

Can't load Features.Diagnostics

I'm creating a web client for joining Teams meetings with the ACS Calling SDK.
I'm having trouble loading the diagnostics API. Microsoft provides this page:
https://learn.microsoft.com/en-us/azure/communication-services/concepts/voice-video-calling/call-diagnostics
You are supposed to get the diagnostics this way:
const callDiagnostics = call.api(Features.Diagnostics);
This does not work.
I am loading the Features like this:
import { Features } from '#azure/communication-calling'
A statement console.log(Features) shows only these four features:
DominantSpeakers: (...)
Recording: (...)
Transcription: (...)
Transfer: (...)
Where are the Diagnostics??
User Facing Diagnostics
For anyone, like me, looking now...
ATOW, using the latest version of #azure/communication-calling SDK, the documented solution, still doesn't work:
const callDiagnostics = call.api(Features.Diagnostics);
call.api is undefined.
TL;DR
However, once the call is instantiated, this allows you to subscribe to changes:
const call = callAgent.join(/** your settings **/);
const userFacingDiagnostics = call.feature(Features.UserFacingDiagnostics);
userFacingDiagnostics.media.on("diagnosticChanged", (diagnosticInfo) => {
console.log(diagnosticInfo);
});
userFacingDiagnostics.network.on("diagnosticChanged", (diagnosticInfo) => {
console.log(diagnosticInfo);
});
This isn't documented in the latest version, but is under this alpha version.
Whether this will continue to work is anyone's guess ¯\(ツ)/¯
Accessing Pre-Call APIs
Confusingly, this doesn't currently work using the specified version, despite the docs saying it will...
Features.PreCallDiagnostics is undefined.
This is actually what I was looking for, but I can get what I want by setting up a test call asking for the latest values, like this:
const call = callAgent.join(/** your settings **/);
const userFacingDiagnostics = call.feature(Features.UserFacingDiagnostics);
console.log(userFacingDiagnostics.media.getLatest())
console.log(userFacingDiagnostics.network.getLatest())
Hope this helps :)
Currently the User Facing Diagnostics API is only available in the Public Preview and npm beta packages right now. I confirmed this with a quick test comparing the 1.1.0 and beta packages.
Check the following link:
https://github.com/Azure-Samples/communication-services-web-calling-tutorial/
Features are imported from the #azure/communication-calling,
for example:
const {
Features
} = require('#azure/communication-calling');

ag-grid-community vs ag-grid-enterprise new Grid

I have a Node client-side application with the latest ag-grid version.
I was using ag-grid-community without any issues with this require line
const {Grid} = require('ag-grid-community');
and this new
new Grid(agGridDiv, agGridOptions);
but if I change the require to
const {Grid} = require('ag-grid-enterprise');
the new fails with exception 'Grid is not a constructor'
How can I fix this? I have tried various changes such as new Grid.Grid etc but nothing seems to work.
For latest 23.1.1 version this page:
// ECMA 5 - using nodes require() method
const AgGrid = require('ag-grid-enterprise');
Another way to follow this guide, it all depends on which repository you download the dependencies from.
import {Grid, GridOptions} from '#ag-grid-community/core';
import {LicenseManager} from '#ag-grid-enterprise/core';
// or
const {Grid, GridOptions} = require('#ag-grid-community/core');
I used core and it worked for import.
For old version:
Grid, like everything else, needs to be imported from ag-grid-community.
1) ag-grid-enterprise is pure additive functionality for ag-grid-community.
2) You will use ag-grid-enterprise via the ag-grid-community api not explicit. Use ag-grid-enterprise for LicenseManager only.
Off-topic:
I would recommend starting with the old version, since the source code of the new version is minified and it will be more difficult for you to understand many nontrivial nuances.

Mongoose schema.post events broken in versions >= 4.8.0

In a node app that uses mongoose, I defined some mongoose middleware hooks like this:
mongoose.model('MyModel').schema.post('save', function(document) {
// following is executed in 4.7.9 but not 4.8.0
console.log('saved');
});
These work perfectly well in mongoose versions up to and including 4.7.9 but if I update mongoose to 4.8.0 in my application, then with no other changes, these hooks fail to be invoked at all. There are no warnings or errors.
http://mongoosejs.com/docs/middleware.html suggests that the way to define these hooks hasn't changed. Is there something different I need to do in 4.8.0+ to retain this behaviour?
I'm pretty sure that you need to declare middleware before creating the model:
let MySchema = new mongoose.Schema(...);
MySchema.post('save', ...);
let MyModel = mongoose.model('MyModel', MySchema);
See this as well: https://github.com/Automattic/mongoose/issues/4971#issuecomment-279238187

Node require executes code twice for mongoose Schemas

I am having trouble with require executing my code twice. Working on a standard Express app I build Mongoose Schemas, each in it's own files and export them.
//user.js
const User = mongoose.model('User', userSchema)
module.exports = User
//In other files
const User = require('../models/User')
Now I use this in two places in my application and get an error saying that
Cannot overwrite `User` model once compiled.
So the code above is getting called twice as it is the only code right now creating a model. However I would expect Node to only execute it once since it is required in my code.
The really strange part is that checking out an earlier version from Git I get the same error and people working with me on this get the same error. So I have no more ideas where to look for solutions.
Found the solution now.
Turns out I required the module once as models/user and once as model/User which in the cache of require creates two separate modules.
There have been many discussion about this:
one issue
another issue
old PR
It seems that this is due to Windows resolving paths case insensitive while other systems resolve paths case sensitive and node therefore doing it sensitive.
And a new module of'cause gets executed. Simply requiring is both times spelled in lowercase solved the issue.
I think the problem is in "const" that you use to declare the variable "User".
Try to use "var" instead of .
//user.js
var User = mongoose.model('User', userSchema)
module.exports = User
//In other files
var User = require('../models/User')
P/S: This is link that clarify more about "const" and "var":
Const in javascript? When to use it and is it necessary
Hope it helpful for you !

Get maxSatisfying version including pre-release with node semver

How can i get the latest version (including the pre-releases) from an array of versions using node/semver.
For example:
var semver = require("semver");
var versions = ["1.0.0-rc.10","1.0.0-rc.11"];
console.log(semver.maxSatisfying(versions, "*"));
Returns null, but i want to get the 1.0.0-rc.11 back.
Kind Regards and have a nice day!
You need to add the includePrerelease option to your maxSatisfying method as described here: https://github.com/npm/node-semver#functions
In your example:
var semver = require("semver");
var versions = ["1.0.0-rc.10","1.0.0-rc.11"];
console.log(semver.maxSatisfying(versions, "*", {
includePrerelease: true
}));
Okay i've found a solution.
The node module semver-extra adds some nice extra functions to the semver library, also one for getting the max version including the pre-releases.
https://www.npmjs.com/package/semver-extra

Resources