DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
        • Changelog
        • Core
        • Map Plugin
        • Shims
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-validate-legacy

  • npm package badge
  • Star
  • Edit on GitHub

A plugin for CanJS that wraps any validation library to can.validate. Can-Validate doesn't do any validation of its own but instead provides some abstraction to your library of choice. The chosen library is registered with can-validate using a shim.

Object

Can-Validate provides methods that can be used to validate values regardless of the validation library used.

var validate = require("can-validate-legacy");
require("can-validate-legacy/shims/validatejs.shim");

var user = {
    firstName: "juan"
};

var constraints = {
    firstName: {
        required: true,
        format: {
            pattern: /^[A-Z].*/,
            message: "^ must be proper cased."
        }
    }
};

var errors = validate.validate(user, constraints);

Can-Validate can be used in two ways, in a can-map instance or standalone.

Usage

The module should require the following files

import 'can-validate-legacy';
import 'validate.js';
import 'can-validate-legacy/shims/validatejs.shim';

Now, can-validate can be used in two ways, either in a can-map or standalone.

Can-Map Plugin Usage

Using the plugin for can-map requires the can-map-define plugins as well.

import 'can-validate-legacy/map/validate/validate';
import 'can/map/define/define';

All can-maps created in the module will now have an augmented setter that checks properties as they are set.

var ViewModel = Map.extend({
  define: {
    name: {
      value: '',
      validate: {
        required: true
      }
    }
  }
});
var viewModel = new ViewModel({});
//
viewModel.validate();
// `errors` will have an error because the `name` value is empty
//  and required is true.
viewModel.attr('errors'); //> Returns the raw response from validation library
viewModel.attr('name', 'Juan');
viewModel.attr('errors'); // => Errors is now empty!

Standalone Usage

First, make sure the correct files are required.

// can.validate is now available
import 'can-validate-legacy';
// Substitute with your library of choice
import 'validate.js';
// If not using ValidateJS, then you'll need a custom shim
import 'can-validate-legacy/shims/validatejs.shim';

Now, we can validate many or a single property. Let's start with the following values and constraints:

var user = {
    firstName: "juan",
    lastName: "Orozco"
};

var constraints = {
    firstName: {
        required: true,
        format: {
            pattern: /^[A-Z].*/,
            message: "^ must be proper cased."
        }
    },
    lastName: {
        required: true,
        format: {
            pattern: /^[A-Z].*/,
            message: "^ must be proper cased."
        }
    }
};

To validate many properties, just run...

var errors = validate.validate(user, constraints);

The once method allows validating just a single value.

var errors = validate.once(user.firstName, constraints.firstName, 'firstName');

Shims and Validation Libraries

A shim registers a validation library with can-validate. It also processes properties and values into a structure more acceptable by the validation library. This allows consuming libraries to switch validation libraries simply by switching out their shim - any new behavior or legacy behavior can be baked into the new shim.

Don't have a library of choice? Can.Validate ships with a shim for ValidateJS.

Change Log

A change log is maintained here.

Contributing

Want to contribute? Read the contributing guide to start.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.14.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news