Socket
Socket
Sign inDemoInstall

demand

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    demand

Form error handling for dietjs


Version published
Weekly downloads
15
increased by650%
Maintainers
1
Install size
12.2 kB
Created
Weekly downloads
 

Readme

Source

Demand module

Form error handling for dietjs

How it works?

  • You can demand request.body[..] values to match criterias like existence, length etc..
  • The module is accessible in every POST request
  • The module can be accessed with the request.demand function
  • The module accepts unlimited agruments, each argument by right goes deeper in request.body
  • If request.passed is true than the form request has passed
  • If request.passed is false then request.errors contains the errors in JSON
  • You can answer if
    • request.passed with response.success(data)
    • else with response.error(data)
    • data is optional

Example

When a POST request comes in (from a form or ajax request) with these parameters:

	{
		'account':{
			username:'adam',
			password:'123456',
		},
		'remain_logged_in': true
	}

You can check if everything is ok with the submitted data with request.check:

var app = new Application(options);

app.get('/login', function(request, response, mysql){
	// DEMAND values to be specific
	request.demand('username').length(0,40);
	request.demand('email').length(0,40).isEmail();
	request.demand('password').length(0,40).equals(request.body.password_again);
	request.demand('options', 'remain_logged_in').isBoolean().length(0,1);
	
	// IF request has passed 
	if(request.passed){ 
		response.success(); // { passed: true, errors: false}
		mysql.end();
	} else {
		response.error(); // { passed: true, errors: [{..},{..}]}
		mysql.end();
	}
});

Demand Functions

FunctionConditionExampleUse Case
isregex/([0-9]+)/irequest.demand('id').is(/([0-9]+)/i)
issetdefinedundefined vs hell worldrequest.demand('message').isset()
isArrayarray[1,3,5,7]request.demand('users').isArray()
isNumberinteger8080request.demand('birth_day').isNumber()
isBooleanbooleantrue or falserequest.demand('agree').isBoolean()
isTextalphaA simple textrequest.demand('username').isText()
isStringstring52 people likes you today!request.demand('message').isString()
isSlugslugseomthing_like_this_842request.demand('username').isSlug()
isEmailemailme@email.comrequest.demand('email').isEmail()
isURLurlhttp://example.com/?p=10request.demand('personal_blog').isUrl()
lengthrangehello is 4request.demand('tweet').length(0, 140)
equalscomparisoncomparing value a with brequest.demand('agree').equals('true')

Register Errors

  • request.error(field, message) - both attributes are required

Respond with JSON

  • response.success(data)
    • the default response value is { passed: true, errors: false}
    • data is appended to the default json response but it's optional
  • response.error(data)
    • the default response value is { passed: false, errors: [...]}
    • data is appended to the default json response but it's optional
    • errors contain a list of erros with

Version History

v0.2
  • added: request.error(field, message)
  • added: response.success(data)
  • added: response.error(data)
v0.1.5
  • added Echo to arguments for multi language isset demading
  • Several Bug Fixes
v0.1
  • First Release

Keywords

FAQs

Last updated on 14 Dec 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc