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

Refactor, can-validate supplies utilities for other validate modules #48

Merged
merged 6 commits into from
Jan 23, 2017

Conversation

Macrofig
Copy link
Contributor

Resolves #42

Added formatErrors

formatErrors takes an errors value and converts it to a more formal structure. The errors value must match any of the types defined in the errors docs.

Added docs and tests for formatErrors.

Added types

Added Errors, Error, and Validator types documentation.

Removed all kinds of stuff

  • The validatejs shim is now in can-validate-validatejs. Note that the APIs have been changed.
  • The can.map plugin still exists in can-validate-legacy.
  • Although the can.map plugin is no longer supported, there is a new plugin for can-define maps that will take its place. Plugin is in progress.

- Improve handling of missing key for errors when object format is desired
- Impove handling of different and mised errors types in raw errors.
var Construct = require('can-construct');
var validate = require('can-validate/lib/validate');
var helpers = require('can-validate/lib/helpers');
var formatErrors = require('can-validate/format-errors/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use paths that end with / so other module loaders can load files. Use can-validate/format-errors/format-errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably wouldn't have another module if this is all can-validate exports. I'd move this code into can-valdiate.js


Shared utilities and type definitions to process validation errors.

@option {function} formatErrors Formats errors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be documented independently, not in here. Plus there should be a @type describing what this module exports, which is an object.


@body

## Utility Methods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is introduced w/o any context, which doesn't "read" well.


An object that defines a validation failure.

@option {string} message A reason why value is in an invalid state.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@type should be used. It should include an example of the object.


The expected response from a validator if a value fails validation.

@option {undefined|null} Expected when value passes validation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null isn't above. Use @type instead of @option here.

errors = [errors];
}
each(errors, function (error) {
resp = resp.concat(parseErrorItem(error));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is slow compared to using [].push.apply( resp, parseErrorItem(error) ). I'm not a fan of resp. It's so generic.

QUnit.test('formatErrors to flat',function(){
var errors = formatErrors(errorObject, 'flat');
QUnit.equal(errors.length, 4);
QUnit.equal(errors[0], numberString);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing mistakes

var errors = formatErrors(errorObject, 'object');
QUnit.ok(errors.name);
QUnit.equal(errors.name.length, 2);
QUnit.equal(errors.name[0], requireString);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing

QUnit.ok(validate.validations.test === noop);
QUnit.test('formatErrors to flat',function(){
var errors = formatErrors(errorObject, 'flat');
QUnit.equal(errors.length, 4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's good to have a 3rd argument explaining what's being tested.

QUnit.test('formatErrors normalizes different error types', function () {
var errors = formatErrors(errorObject);
QUnit.ok(errors);
QUnit.equal(errors.length, 4);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should really use QUnit.deepEqual here for better testing accuracy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QUnit.deepEqual on the whole errors response like:

QUnit.deepEqual( errors, [{ ... }, { ... } ])

- Moved formatErrors. Updated tests.
- Improved docs based on PR comments
@Macrofig Macrofig merged commit 1a81fe0 into master Jan 23, 2017
@Macrofig Macrofig deleted the 42-refactor-format-errors-helpers branch April 6, 2017 19:17
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

Successfully merging this pull request may close these issues.

None yet

2 participants