const baseHandler: APIGatewayProxyHandlerV2 = async (event) => {
return service.create(event.body);
}
const inputSchema = {
type: "object",
properties: {
body: {
type: "object",
properties: {
year: { type: "number" },
questionId: { type: "string" },
propSeq: { type: "number" },
questionTitle: { type: "string" },
propContent: { type: "string" },
isTrue: { type: "boolean" },
chapter: { type: "number" }
},
required: ["year", "questionId", "propSeq", "questionTitle", "propContent", "isTrue", "chapter"],
},
},
};
export const handler = middy(baseHandler)
.use(jsonBodyParser())
.use(validator({inputSchema}))
.use(httpErrorHandler())
I'm writing AWS Lambda code on Serverless Framework.
I wanted request body validator like express-validator, so I found middy.
But it looks impossible to validate the length of something.
I want to force the length of year to 4.
for example, 2023(o), 23(x)
properties: {
year: { type: "number", length: 4 }
}
As you guess, length property cannot be understood.
I don't want to add some codes to baseHandler function to validate the length.
Thank you in advance.
Middy uses JSONSchema, so you can use anything that is compatible with JSONSchema there. You could use length, but then you'd need to switch the type to string from number, as length is not supported for number (rightfully so in my opinion). If you want to keep it as number, then probably using range-based validation is your best bet: https://json-schema.org/understanding-json-schema/reference/numeric.html#range
Related
Using AWS CDK 2 for creating schemas and tables, I seem to have problems linking the schemaReference
const schema = new glue.CfnSchema(this, "User", {
compatibility: "NONE",
dataFormat: "JSON",
name: "user",
schemaDefinition: JSON.stringify(userSchema),
});
new glue.CfnTable(
this,
"UserTable",
{
catalogId: this.account,
databaseName: "my_db",
tableInput: {
name: "users",
tableType: "EXTERNAL_TABLE",
storageDescriptor: {
location: "my_db.public.users",
schemaReference: schema,
},
parameters: {
classification: "postgresql",
typeOfData: "table",
connectionName: "rds_conn",
},
},
}
);
It seems like I'd expect schemaReference to be able to use the Cfn output in some way? I can only get this working by hard coding a schemaReference object with a schemaVersionId that I find in the console.
My solution was to lock the schema version in its definition, then to reference the schema by name. Example
new glue.CfnSchema(this, "User", {
name: "user",
// ...
checkpointVersion: {
versionNumber: 1,
},
});
new glue.CfnTable(
this,
"UserTable",
{
// ...
tableInput: {
// ...
storageDescriptor: {
// ...
schemaReference: {
schemaId: {
registryName: "default-registry",
schemaName: "user",
},
schemaVersionNumber: 1,
},
},
},
}
);
Though verbose, it has the advantage of being portable across stacks.
Im implementing a mongodb search.
The search performs a find on field values:
[{
value: "my.string.here"
}, {
value: "my other here"
}{
...
}]
When i enter "my" both entries are found. What have my query to look like to ignore the dots on the first entry? So when i enter "my string" the first element gets returned?
Actually it works only when i enter "my.string" which is not nice.
let limit = Number(req.query.limit || 100);
let skip = Number(req.query.skip || 0);
collection.find({
$or: [{
value: new RegExp(req.body.search, "gi")
}, {
tags: {
$in: req.body.search.split(",").map((val) => {
return new RegExp(val, "gi")
})
}
}]
}).skip(skip).limit(limit).toArray((err, result) => {
if (err) {
res.status(500).json(err);
} else {
res.status(200).json(result);
}
});
EDIT:
A solution could look like this:
let query = {
$or: [{
name: new RegExp(req.body.search, "gi")
}, {
tags: {
$in: req.body.search.split(",").map((val) => {
return new RegExp(val, "gi")
})
}
}, {
name: new RegExp(req.body.search.split(' ').join('.'), "gi")
}, {
name: new RegExp(req.body.search.split(' ').join('_'), "gi")
}, {
name: new RegExp(req.body.search.split(' ').join('-'), "gi")
}]
};
But i find it ugly and not elegant. Is there a better way to do this ?
guys, I m new to loopback, can anybody tell me what I m doing wrong below is my code
Permissiontb.assembleAndInsert = async (ctx, cb) => {
console.log(ctx.args.data)
console.log(ctx.res.body)
};
Permissiontb.remoteMethod('assembleAndInsert', {
http: {
path: '/assembleAndInsert',
verb: 'post',
},
accepts: [{ arg: 'data', type: 'object', http: { source: 'context' } },
{"arg": "options", "type": "object", "http": "optionsFromRequest"}],
returns: {
arg: 'data',
type: 'object',
},
});
now my problem is that if i does console.log(ctx.res.body) i got null can anybody tell me what i m doing wrong
Please refer Link
You can use ctx.req.body
FYI : You can log ctx.req and see all the available data in it.
I am new to mustache. Have an object like
object = [{
name: 'A',
fields: { type: "string" }
},
{
name: 'B',
fields: { type: "boolean", default: false }
}]
I am passing that object to mustache template and I want generated code where default values are also shown.
Code:
{{#object}}
var {{name}}: {{#fields}} {{type}} {{^default}} = {{default}} {{/default}}{{/fields}}
{{/object}}
But I am not able to get the expected output from above code.
Expected Output:
var A: string
var B: boolean = false
If default were the string "false" it would (almost) work. You've currently got the equivalent to:
default = false
if (!default) {
" = " + default
}
… but you need:
default = "false"
if (default) {
" = " + default
}
So this would do it for you:
object = [{
name: 'A',
fields: { type: "string" }
},
{
name: 'B',
fields: { type: "boolean", default: "false" }
}]
… and change the {{^default}} block to {{#default}}:
{{#object}}
var {{name}}: {{#fields}} {{type}} {{#default}} = {{default}} {{/default}}{{/fields}}
{{/object}}
this work for me:
object : [
{
name: 'A',
fields: { type: "string" }
},
{
name: 'B',
fields: { type: "boolean", default: "false"}
}
]
with
{{#object}}
var {{name}}: {{#fields}} {{type}} {{#default}} = {{default}} {{/default}}{{^default}}{{/default}}{{/fields}}
{{/object}}
if the false cannot change to "false" and you want a result like you show above,you may try:
object : [
{
name: 'A',
fields: { type: "string", default: true}
},
{
name: 'B',
fields: { type: "boolean", default: false}
}
]
with
{{#object}}
var {{name}}: {{#fields}} {{type}} {{#default}}{{/default}}{{^default}}= false{{/default}}{{/fields}}
{{/object}}
Is it possible to set what field should be the "id" field? I'm defining my scheme with:
var Person = app.ormDb.define('person', {
id : { type: String, index: true, default: function () { return uuid.v4(); } },
oAuthID : { type: String, index: true },
name : { type: String },
gender : { type: String },
birth : { type: Date },
email : { type: String },
imageID : { type: String },
favorites : { type: JSON, default: function() { return {cars : [], animals : []}; } },
date : { type: Date, default: function() { return new Date(); } },
updated : { type: Date, default: function() { return new Date(); } }
});
and my defined id field shows up in MongoDB but when I use jugglingdb to lookup a person the returned value for Person.id is the MongoDB _id ObjectId value. so my id is hidden.
_id is reserved in MongoDB as the primary key. This would explain why it can't be changed. You can place any value into this as long as it's unique. Hope that helps.
There is also currently an open issue in the jugglingdb-mongodb adapter. That addresses the reason why id is not returned. Basically if object.id exists when calling create it is removed before insert into the collection.