moleculer add validation rule on date format - moleculer

I have below api created by moleculer framework, there is EventDate parameter, how can I specify the format and apply validation rule to check receiving date
createEvent: {
params: {
UserId: {
type: "string",
optional: false
},
Name: {
type: "string",
optional: false
},
Description: {
type: "string",
optional: false
},
Location: {
type: "string",
optional: false
},
EventDate: {
type: "string",
optional: false
}
},
handler(ctx) {
let entity = ctx.params;
return this.broker.call("event.find", {
query: {
UserId: entity.UserId,
Name: entity.Name,
}
}).then((res) => {
if (res == null || res.length == 0) {
return this.broker.call("event.create",{
UserId:entity.UserId,
Location: entity.Location,
EventDate: entity.EventDate,
Description: entity.Description,
Name:entity.Name
}).then(doc =>{
return new Response(200, 'success', doc);
});
} else {
throw new ValidationError("you already created event with same name", -1, "you already created event with same name");
}
});
}
},
I want to accept this date format only yyyy/mm/dd

If you want to accept only string date in "yyyy/mm/dd" format, use pattern in string validator.
E.g.:
EventDate: {
type: "string",
pattern: /([12]\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01]))/g,
optional: false
}

Related

How to do Prisma runtime model validation?

In my application, I have validated the input credential at the DTO level by using class-validator. But I need runtime model validation like sequelize ORM.
In sequelize:
'use strict';
import { DataTypes, Sequelize } from 'sequelize';
function User(sequelize: Sequelize) {
const user = sequelize.define(
'User',
{
name: {
type: DataTypes.STRING,
allowNull: false
},
role: {
type: DataTypes.STRING(20),
allowNull: false
},
email: {
type: new DataTypes.STRING,
allowNull: false,
validate: {
isEmail: {
// args: true,
msg: 'Invalid email'
},
len: {
args: [1, 100] as readonly [number, number],
msg: 'Email length should be 1 to 100 characters'
},
notNull: {
// args: true,
msg: 'Email cannot be empty'
}
}
},
password: {
type: DataTypes.VIRTUAL,
allowNull: true,
},
},
{
tableName: 'users',
underscored: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true
}
);
return user;
}
export default User;
Is there any possibility to do model validation in Prisma?
There is an open feature request for Prisma to support runtime model validation directly at the Schema level. Alternatively, you can leverage the Client Extensions to perform validation. There is an example in this blog post that shows how to perform custom runtime validation.

Fulltext mongodb $text search query in graphql-compose-mongoose

I'm unable to figure out how to construct a graphql query for performing the mongodb fulltext search using the text index. https://docs.mongodb.com/manual/text-search/
I've already created a text index on my string in the mongoose schema but I don't see anything in the schemas that show up in the grapqhl playground.
A bit late, though I was able to implement it like so
const FacilitySchema: Schema = new Schema(
{
name: { type: String, required: true, maxlength: 50, text: true },
short_description: { type: String, required: true, maxlength: 150, text: true },
description: { type: String, maxlength: 1000 },
location: { type: LocationSchema, required: true },
},
{
timestamps: true,
}
);
FacilitySchema.index(
{
name: 'text',
short_description: 'text',
'category.name': 'text',
'location.address': 'text',
'location.city': 'text',
'location.state': 'text',
'location.country': 'text',
},
{
name: 'FacilitiesTextIndex',
default_language: 'english',
weights: {
name: 10,
short_description: 5,
// rest fields get weight equals to 1
},
}
);
After creating your ObjectTypeComposer for the model, add this
const paginationResolver = FacilityTC.getResolver('pagination').addFilterArg({
name: 'search',
type: 'String',
query: (query, value, resolveParams) => {
resolveParams.args.sort = {
score: { $meta: 'textScore' },
};
query.$text = { $search: value, $language: 'en' };
resolveParams.projection.score = { $meta: 'textScore' };
},
});
FacilityTC.setResolver('pagination', paginationResolver);
Then you can assign like so
const schemaComposer = new SchemaComposer();
schemaComposer.Query.addFields({
// ...
facilities: Facility.getResolver('pagination')
// ...
});
On your client side, perform the query like so
{
facilities(filter: { search: "akure" }) {
count
items {
name
}
}
}

Fuzzy search using mongoose from vue client

Getting error unknown top level operator $regex
search.vue `
let questDocuments = await conversation
.find({ query: { $limit: 100, $search: q, skippop: true } })
.then(response => {`
q is the string being passed
service hook
before: {
all: [],
find: [
hookBeforeFind,
search({
fields: ["label"],
deep: true
})
],
Model
const conversation = new Schema(
{
label: { type: String, required: true },
nodeId: { type: String, required: true },
details: { type: String },
url: { type: String },
creator: { type: String },
handle: { type: String },
date: { type: String },
From search bar add expression to search. E.g "the"
Add $regex to the whitelist option of the Mongoose service:
app.use('/messages', service({
Model,
whitelist: [ '$regex' ]
}));
try this
// regex to find records that start with letter any name , example "e"
Model.aggregate([
{
$match: {
field_name: {
$regex: "^" + searchName,
$options: "i"
}
}
}]).exec(function(err, result) {
if (err) { // handle here }
if (result) { // do something }
}

Elastic Search when to add dynamic mappings

I've been having troubles with Elastic Search (ES) dynamic mappings. Seems like I'm in a catch-22. https://www.elastic.co/guide/en/elasticsearch/guide/current/custom-dynamic-mapping.html
The main goal is to store everything as a string that comes into ES.
What I've tried:
In ES you can't create a dynamic mapping until the index has been
created. Okay, makes sense.
I can't create an empty index, so if
the first item sent into the index is not a string, I can't
re-assign it... I won't know what type of object with be the first
item in the index, it could be any type, due to how the the app accepts a variety of objects/events.
So if I can't create the mapping ahead of time, and I can't insert an empty index to create the mapping, and I can't change the mapping after the fact, how do I deal with the first item if its NOT a string???
Here's what I'm currently doing (using the Javascript Client).
createESIndex = function (esClient){
esClient.index({
index: 'timeline-2015-11-21',
type: 'event',
body: event
},function (error, response) {
if (error) {
logger.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
console.log(logger.SEVERITY.ERROR, 'acceptEvent elasticsearch create failed with: '+ error + " req:" + JSON.stringify(event));
res.status(500).send('Error saving document');
} else {
res.status(200).send('Accepted');
}
});
}
esClientLookup.getClient( function(esClient) {
esClient.indices.putTemplate({
name: "timeline-mapping-template",
body:{
"template": "timeline-*",
"mappings": {
"event": {
"dynamic_templates": [
{ "timestamp-only": {
"match": "#timestamp",
"match_mapping_type": "date",
"mapping": {
"type": "date",
}
}},
{ "all-others": {
"match": "*",
"match_mapping_type": "string",
"mapping": {
"type": "string",
}
}
}
]
}
}
}
}).then(function(res){
console.log("put template response: " + JSON.stringify(res));
createESIndex(esClient);
}, function(error){
console.log(error);
res.status(500).send('Error saving document');
});
});
Index templates to the rescue !! That's exactly what you need, the idea is to create a template of your index and as soon as you wish to store a document in that index, ES will create it for you with the mapping you gave (even dynamic ones)
curl -XPUT localhost:9200/_template/my_template -d '{
"template": "index_name_*",
"settings": {
"number_of_shards": 1
},
"mappings": {
"type_name": {
"dynamic_templates": [
{
"strings": {
"match": "*",
"match_mapping_type": "*",
"mapping": {
"type": "string"
}
}
}
],
"properties": {}
}
}
}'
Then when you index anything in an index whose name matches index_name_*, the index will be created with the dynamic mapping above.
For instance:
curl -XPUT localhost:9200/index_name_1/type_name/1 -d '{
"one": 1,
"two": "two",
"three": true
}'
That will create a new index called index_name_1 with a mapping type for type_name where all properties are string. You can verify that with
curl -XGET localhost:9200/index_name_1/_mapping/type_name
Response:
{
"index_name_1" : {
"mappings" : {
"type_name" : {
"dynamic_templates" : [ {
"strings" : {
"mapping" : {
"type" : "string"
},
"match" : "*",
"match_mapping_type" : "*"
}
} ],
"properties" : {
"one" : {
"type" : "string"
},
"three" : {
"type" : "string"
},
"two" : {
"type" : "string"
}
}
}
}
}
}
Note that if you're willing to do this via the Javascript API, you can use the indices.putTemplate call.
export const user = {
email: {
type: 'text',
},
};
export const activity = {
date: {
type: 'text',
},
};
export const common = {
name: {
type: 'text',
},
};
import { Client } from '#elastic/elasticsearch';
import { user } from './user';
import { activity } from './activity';
import { common } from './common';
export class UserDataFactory {
private schema = {
...user,
...activity,
...common,
relation_type: {
type: 'join',
eager_global_ordinals: true,
relations: {
parent: ['activity'],
},
},
};
constructor(private client: Client) {
Object.setPrototypeOf(this, UserDataFactory.prototype);
}
async create() {
const settings = {
settings: {
analysis: {
normalizer: {
useLowercase: {
filter: ['lowercase'],
},
},
},
},
mappings: {
properties: this.schema,
},
};
const { body } = await this.client.indices.exists({
index: ElasticIndex.UserDataFactory,
});
await Promise.all([
await (async (client) => {
await new Promise(async function (resolve, reject) {
if (!body) {
await client.indices.create({
index: ElasticIndex.UserDataFactory,
});
}
resolve({ body });
});
})(this.client),
]);
await this.client.indices.close({ index: ElasticIndex.UserDataFactory });
await this.client.indices.putSettings({
index: ElasticIndex.UserDataFactory,
body: settings,
});
await this.client.indices.open({
index: ElasticIndex.UserDataFactory,
});
await this.client.indices.putMapping({
index: ElasticIndex.UserDataFactory,
body: {
dynamic: 'strict',
properties: {
...this.schema,
},
},
});
}
}
wrapper.ts
class ElasticWrapper {
private _client: Client = new Client({
node: process.env.elasticsearch_node,
auth: {
username: 'elastic',
password: process.env.elasticsearch_password || 'changeme',
},
ssl: {
ca: process.env.elasticsearch_certificate,
rejectUnauthorized: false,
},
});
get client() {
return this._client;
}
}
export const elasticWrapper = new ElasticWrapper();
index.ts
new UserDataFactory(elasticWrapper.client).create();

Unable to load data from a json file in sencha touch 2

I've trying to get the sencha touch 2 data management examples to work but with no use. Here is the code of a simple model and store that are not working (getCount returns 0).
Ext.define('MyClient.model.Product', {
extend:'Ext.data.Model',
config:{
fields:['name', 'image'],
proxy:{
type:'ajax',
url:'http://localhost/st2/Projets/my-client-sencha/data/products.json',
reader:{
type:'json',
rootProperty:'products',
successProperty:'success'
}
}
}
});
Ext.define('MyClient.store.ProductsStore', {
extend:'Ext.data.Store',
config:{
model:'MyClient.model.Product',
autoLoad:true,
autoSync:true
}
});
In the launch function I have these lines:
var prod = Ext.create('MyClient.store.ProductsStore');
prod.load();
alert(prod.getCount());
And finally here's my products.json:
[
{
"name":"test"
}
]
I'm not getting any errors in the console but still the getCount always returns 0. Can use some help here please.
EDIT: wrong JSON, not working with this neither:
{
"success":true,
"products": [
{
"name":"test"
}
]
}
Because of your setting rootProperty:'products', your json has to be like
{
products: [
{
"name":"test"
}
]
}
if you do not want to change server response remover rootProperty from config.
have a look at Json Reader doc
Ahh... you forgot about asyn nature of the load()....
var prod = Ext.create('MyClient.store.ProductsStore');
prod.load(function ( ){
alert(prod.getCount());
});
Notice that it prod.load() is using only for testing purposes, as far you have set property autoLoad: true.
In your snippet the loader would make 2 similar calls.
Cheers, Oleg
Ext.define('MyBizilinkms.model.Customer', {
extend: 'Ext.data.Model',
config: {
identifier:'uuid',
fields: [
'CustId',
'EMail',
'Title',
'FName',
'MdInitial',
'LName',
'PhnNum',
'SecondPhnNo',
'DOB',
'Address',
'SecondAddress',
'City',
'State',
'Zip',
'Country',
'RecieveEmail',
'IsSaveonServer',
{
name: 'Full_Name',
type:'string',
convert:function(v, rec) {
return rec.data.FName + " " + rec.data.LName;
}
}],
validations: [
{
type: 'format',
name: 'EMail',
matcher: /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
message:"Valid Email Required"
},
{
name: 'PhnNum',
type : 'custom',
message : "Valid Phone required",
validator : function(config, value, model) {
var reg = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
return reg.test(value);
}
},
{
name: 'SecondPhnNum',
type : 'custom',
message : "Valid 2nd Phone required",
validator : function(config, value, model) {
if (!Ext.isEmpty(value)) {
var reg = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
return reg.test(value)
}
return true;
}
},
{
type: 'presence',
name: 'FName',
message : "First Name is required"
},
{
type: 'presence',
name: 'LName',
message : "Last Name is required"
},
{
type: 'presence',
name: 'Address',
message : "Address is required"
},
{
type: 'presence',
name: 'City',
message : "City is required"
},
{
name: 'State',
type : 'custom',
message : "Valid State required",
validator : function(config, value, model) {
var reg = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
if(Ext.isEmpty(value))
value = '00'
var state = value.replace(/^\s+|\s+$/g, "");
return reg.test(state)
}
},
{
name: 'Zip',
type : 'custom',
message : "Valid Zip required",
validator : function(config, value, model) {
var reg = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
return reg.test(value)
}
},
{
type: 'presence',
name: 'Country',
message : "Country is required"
}
]
},
getFullName: function() {
return this.get('FName') + '' + this.get( 'LName');
}
});