Skip to content

Commit

Permalink
Cleanup markdown sample code (#48)
Browse files Browse the repository at this point in the history
* fix markdown code snippets

* Use const/let in sample code

* run eslint on markdown code

* run eslint on markdown code
  • Loading branch information
Christopher Baker authored Feb 15, 2018
1 parent faa8d5e commit 139ecf7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 98 deletions.
53 changes: 27 additions & 26 deletions docs/can-define-validate-validatejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ using [validate.js](https://validatejs.org/).
Checks for ValidateJS constraints and attaches useful methods.

```js
var defineValidate = require('can-define-validate-validatejs');
var User = DefineMap.extend({
name: {
validate: {
presence: true
}
}
});
// Attach methods to any instance created of `User`
defineValidate(User);
var user = new User();
user.errors();//-> [{name: ['is required']}]
```
import defineValidate from "can-define-validate-validatejs";
const User = DefineMap.extend( {
name: {
validate: {
presence: true
}
}
} );

// Attach methods to any instance created of `User`
defineValidate( User );
const user = new User();
user.errors();//-> [{name: ['is required']}]
```

@param {Object} Map The [can-define/map/map] constructor. Adds [can-define-validate-validatejs.errors] and [can-define-validate-validatejs.test-set] methods to the prototype of this map.

Expand All @@ -37,32 +38,32 @@ Any validation properties must match the structure used by Validate.JS [constrai
For example:

```js
var User = DefineMap.extend({
name: {
validate: {
presence: true
}
}
});
const User = DefineMap.extend( {
name: {
validate: {
presence: true
}
}
} );
```

Initialize the validators on the Define Map by calling the `defineValidate` function.

```js
defineValidate(User);
defineValidate( User );
```

When an instance is created, the instance will have validation properties that can be used in other modules or in templates

In a module:

```js
var user = new User();
const user = new User();

var onSubmit = function () {
if (user.errors()) {
alert('Cannot continue, please check form for errors');
}
const onSubmit = function() {
if ( user.errors() ) {
alert( "Cannot continue, please check form for errors" );
}
};
```

Expand Down
58 changes: 30 additions & 28 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ The `errors` method retrieves errors from validator.
Returns all errors for the current map instance.

```js
var Person = new DefineMap({
name: {
validate: {
presence: true
}
}
});
var person = new Person();
person.errors();
//-> [{message: "is required", related: "name"}]
```
const Person = new DefineMap( {
name: {
validate: {
presence: true
}
}
} );
const person = new Person();
person.errors();

//-> [{message: "is required", related: "name"}]
```

@return {can-validate.errors} Will return `undefined` if map is valid.
Otherwise, will return an array of [can-validate/types/errors].
Expand All @@ -28,23 +29,24 @@ The `errors` method retrieves errors from validator.
Returns errors for the specified keys from current map instance.

```js
var Person = new DefineMap({
name: {
validate: {
presence: true
}
}
age: {
validate: {
presence: true,
numericality: true
}
}
});
var person = new Person();
person.errors('name');
//-> [{message: "is required", related: "name"}]
```
const Person = new DefineMap( {
name: {
validate: {
presence: true
}
},
age: {
validate: {
presence: true,
numericality: true
}
}
} );
const person = new Person();
person.errors( "name" );

//-> [{message: "is required", related: "name"}]
```

@param {Array<string>} [propName] The property key to retrieve errors for.

Expand Down
90 changes: 46 additions & 44 deletions docs/test-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,35 @@ Tests value changes against constraints. Does not set errors on map instance.
Otherwise, will return an array of [can-validate/types/errors].

```js
var Person = new DefineMap({
name: {
validate: {
presence: true
}
}
});
var person = new Person();
person.testSet();
// returns: [{message: "is required", related: "name"}]
```
const Person = new DefineMap( {
name: {
validate: {
presence: true
}
}
} );
const person = new Person();
person.testSet();

// returns: [{message: "is required", related: "name"}]
```

@signature `map.testSet(keyName, value)`

Changes `keyName`'s value in the map instance clone. Then checks if the object is valid.
```js
var Person = new DefineMap({
name: {
validate: {
presence: true
}
}
});
var person = new Person({name: 'Juan'});
person.testSet('name', '');
//=> [{message: "is required", related: "name"}]
```
const Person = new DefineMap( {
name: {
validate: {
presence: true
}
}
} );
const person = new Person( { name: "Juan" } );
person.testSet( "name", "" );

//=> [{message: "is required", related: "name"}]
```

@param {string} keyName The property key to test
@param {*} value The new value to test for `keyName`.
Expand All @@ -52,26 +54,26 @@ Tests value changes against constraints. Does not set errors on map instance.
`true` will create a new instance of the map and test changes on the clean instance.

```js
var Person = new DefineMap({
name: {
validate: {
presence: true
}
},
age: {
validate: {
numericality: true
}
}
});
var person = new Person({name: 'Juan', age: 35});

// this returns [{message: "is required", related: "name"}]
person.testSet({name: ''});

//this returns [{message: "is required", related: "name"}]
person.testSet({age: 35}, true);
```
const Person = new DefineMap( {
name: {
validate: {
presence: true
}
},
age: {
validate: {
numericality: true
}
}
} );
const person = new Person( { name: "Juan", age: 35 } );

// this returns [{message: "is required", related: "name"}]
person.testSet( { name: "" } );

//this returns [{message: "is required", related: "name"}]
person.testSet( { age: 35 }, true );
```

@param {object} props An object of key/value pairs, where `key` is a property in
the map instance that will update to the new `value`.
Expand All @@ -91,6 +93,6 @@ being set on the map instance when using `testSet`. This means that errors retur

This behavior can be controlled when testing multiple values by passing `true` for `useNewInstance`. This will test values with a new instance of the map constructor, allowing better control of what values are tested.

```javascript
map.testSet({name: '', age: 100}, true);
```js
map.testSet( { name: "", age: 100 }, true );
```

0 comments on commit 139ecf7

Please sign in to comment.