Search code examples
validationmodelloopback

Loopback custom password validation


very simple question: if I try to validate a password in a User model it seems I can only validate the already encrypted password? So for example if I use

 Customer.validatesLengthOf('password', { min: 8, message: 'Too short' })

Then the encrypted password is checked (which is always longer than 8 characters), so no good... If I try to use a custom validation, how can I get access to the original password (the original req.body.password basically)?


Solution

  • ok, no answer so what I'm doing is using a remote hook to get access to the original plain password and that'll do for now.

    var plainPwd
    Customer.beforeRemote( 'create', function (ctx, inst, next) {
        plainPwd = ctx.req.body.password
        next()
    })
    

    Then I can use it in a custom validation:

    Customer.validate( 'password', function (err, res) {
        const pattern = new RegExp(/some-regex/)
        if (plainPwd && ! pattern.test( plainPwd )) err()
    }, { message: 'Invalid format' })