Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation from scripts #157

Closed
eldoy opened this issue Mar 29, 2024 · 1 comment
Closed

Validation from scripts #157

eldoy opened this issue Mar 29, 2024 · 1 comment

Comments

@eldoy
Copy link
Owner

eldoy commented Mar 29, 2024

We currently have support for validations as objects in validations. These can be used in the request cycle with $.validate.

How do we use them in scripts, outside the request cycle?

$.validate currently throws (halt), we just want to get the errors. If I want to validate some data:

var { validate } = require('waveorb')
var values = { name: 'Baner', email: 'e@xample.com' }
var validator = await validate(app.validations.userCreate, { values })
if (validator.errors) {
  // handle errors
} else {
  // No errors, empty object
}

This cannot use waveorb-validations at the moment, since it depends on $.

If we are actually saving data after this, it feels more natural to use a classic ORM maybe:

var { validate } = require('waveorb')

class Project {
  constructor (values) {
    this.values = values
  }

  valid() {
    var validator = await validate(app.validations.userCreate, { values: this.values })
    return !!validator.errors
  }
}

We could also introduce a validator function which handles the setup:

var { validate } = require('waveorb')

module.exports = function userCreateValidator($, validation, data) {
  if (!$.db && typeof global.db != 'undefined') $.db = global.db
  if (!$.params) $.params = {}
  // ... Add missing stuff for waveorb-validator and '$'

  return validate($, data)(validation)
}
@eldoy
Copy link
Owner Author

eldoy commented Mar 30, 2024

Added a validator function to app.validator

@eldoy eldoy closed this as completed Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant