How to verify that the timestamp send is in UTC in mongoose/Hapijs?

2.4k views Asked by At

I'm building an application in node.js using Hapi and database in Mongodb using Mongoose. I have the following message schema:

var schema = {
    from : {
        type : Schema.ObjectId,
        ref : 'User'
    },
    content : {
        type : String,
        required : true
    },
    group : {
        type : Schema.ObjectId,
        ref : 'Group'
    },
    created : {
        type : Number,
        default : Date.now()
    }
};

I want the created field in the schema to be a timestamp and always be in UTC. For this my client always converts its current timestamp into its UTC timestamp and then sends to the server. However, I can't find a possible way on the server to verify whether the timestamp provided has been converted into UTC or not. Is there any way I could impose that validation in Hapi/Mongoose?

Thanks in advance.

Edit:

  1. Changed the type of created field to be Number since I want to store the timestamp and not the date string.
1

There are 1 answers

0
Clarkie On

Hapi allows you to have a validate property on each route. Under the hood this uses Joi to validate the schema provided in this property to the payload, parameters, query string and headers: Route Configuration

Using this you can validate your created field to a particular date format: date.format()

For example this will validate it to ISO 8601 format:

    validate: {
        payload: {
            created: Joi.date().iso();
        }
    }