Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
  dist
  package.json
  README.md
Size: Mime:
  README.md

Flux Abstract Validator Build Status

Abstract Validation in a Flux-like data flow pattern.

The Abstract Validator

The abstract validator consists of a series of utilites that makes writing validators far easier in a Flux-like data flow pattern.

Initializing

To use the abstract validator, call the function returned from the require with the following arguments:

Name Type Description
blankValidators FieldValidators Validators triggered to verify if fields are blank.
substantiveValidators FieldValidators Validators triggered to verify if fields are correct.
validationOptions Plain object A set of options detailing precisely when validations are triggered. These should not be touched without good reason.

The Validator Type

A Validator is of this type:

type Validator = func(any): (Immutable.List<any> | Array<any>)

The FieldValidators specified above is this type:

type FieldValidator = Immutable.Map<string, Immutable.List<Validator>>

The return type of a Validator represents the results of the validation. These can be strings, for example, that simply describe the error, or functions, for more complex validation results, that allow the user to optionally autocorrect errors.

Using the Abstract Validator

When called with the above arguments, the abstract validator returns an object with three keys:

Key Description
Validator A class wrapper around the pure validator which has been provided with the blank and substantive validators.
ValidatorActions Actions called to notify the validator of the state of the form.
storeChangeEmitter An event emitter that fires every time the set of validations change.

The Validator

# validate(formData: Immutable<string, any>, validateBlankFields: boolean): Immutable.Map<string, Immutable.List<any>>

Performs validation on the provided form data. If validateBlankFields is turned on and fields are blank, the validation for those fields is short-circuited, and only errors for blankness are shown. There is currently no option to turn this short-circuiting off.

# passesValidation(formData: Immutable<string, any>, validateBlankFields: boolean): boolean

Similar to the above, but returns a boolean instead of the full list of errors.

The Validator Actions

# afterFormFieldChanged(name: string, value: any): void

This method should be called with the name and the value of a form field when it is changed.

# afterFormFieldBlurred(name: string): void

This method should be called with the name of a form field when it is blurred.

# onFormFieldDeleted(name: string): void

More complex forms may be subject to change in the course of user interaction. If form fields need to be deleted (thus skipping validation), this method should be called with the name of the field to be deleted.

# onFormSubmit(): void

This method should be called when the form is submitted. Note that form submission triggers changes in the nature of validation.

# onExternalErrorsReceived(errors: Immutable.Map<string, Immutable.List<any>>): void

This method allows external errors to be set on the Validator. It replaces all existing internal errors with the provided external errors. Ideally, of course, there should never be any internal errors before the external request is made.

The Validator Store

storeChangeEmitter

The storeChangeEmitter is a property on the validator store that is fired every time the set of validation messages is changed, e.g. if there are new errors. It is an event emitter, and there is a maximum of one listener on this emitter. To listen to this, simply attach a listener:

storeChangeEmitter.onAny(function(validationMessages) {
    // and so forth.
})

validationMessages is of the same type as the return value of the Validator's validate, Immutable.Map<string, Immutable.List<any>>.