Skip to content

Commit

Permalink
Update paths and messages of errors in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Mar 27, 2020
1 parent ec206e8 commit 98c0de9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/cloning.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ const clonedUser = user.clone(

// Error: Invalid Attributes
// details: [
// { message: '"name" is required', path: 'name' }
// { message: '"name" is required', path: ['name'] }
// ]
```
10 changes: 5 additions & 5 deletions docs/strict-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const user = User.buildStrict({

// Error: Invalid Attributes
// details: [
// { message: '"name" is required', path: 'name' },
// { message: '"age" must be a number', path: 'age' }
// { message: '"name" is required', path: ['name'] },
// { message: '"age" must be a number', path: ['age'] }
// ]
```

Expand Down Expand Up @@ -59,8 +59,8 @@ const book = Book.buildStrict({

// InvalidBookError: Wait, this book is not right
// code: 'INVALID_BOOK'
// details: [
// { message: '"name" is required', path: 'name' },
// { message: '"year" must be a number', path: 'year' }
// errors: [
// { message: '"name" is required', path: ['name'] },
// { message: '"year" must be a number', path: ['year'] }
// ]
```
4 changes: 2 additions & 2 deletions docs/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const { valid, errors } = user.validate();
valid; // false
errors; /*
[
{ message: '"name" length must be at least 10 characters long', path: 'name' },
{ message: '"age" is required', path: 'age' }
{ message: '"name" length must be at least 10 characters long', path: ['name'] },
{ message: '"age" is required', path: ['age'] }
]
*/

Expand Down
11 changes: 5 additions & 6 deletions docs/validation/attribute-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const User = attributes({
password: String,
passwordConfirmation: {
type: String,
equal: { attr: 'password' }
}
})(class User { });
equal: { attr: 'password' },
},
})(class User {});

const user = new User({
name: 'Gandalf',
password: 'safestpasswordever',
passwordConfirmation: 'notthatsafetho'
passwordConfirmation: 'notthatsafetho',
});

const { valid, errors } = user.validate();
Expand All @@ -24,9 +24,8 @@ valid; // false
errors; /* [
{
message: '"passwordConfirmation" must be one of [ref:password]',
path: 'passwordConfirmation'
path: ['passwordConfirmation']
}
]
*/

```
22 changes: 11 additions & 11 deletions docs/validation/nested-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ Structure will validate nested values, including array items validations and nes
const Book = attributes({
name: {
type: String,
required: true
}
})(class Book { });
required: true,
},
})(class Book {});

const User = attributes({
initials: {
type: String,
minLength: 2
minLength: 2,
},
favoriteBook: Book,
books: {
type: Array,
itemType: Book
}
})(class User { });
itemType: Book,
},
})(class User {});

const user = new User({
initials: 'A',
favoriteBook: new Book(),
books: [new Book()]
books: [new Book()],
});

const { valid, errors } = user.validate();

valid; // false
errors; /*
[
{ message: '"initials" length must be at least 2 characters long', path: 'initials' },
{ message: '"name" is required', path: 'favoriteBook.name' },
{ message: '"name" is required', path: 'books.0.name' }
{ message: '"initials" length must be at least 2 characters long', path: ['initials'] },
{ message: '"favoriteBook.name" is required', path: ['favoriteBook', 'name'] },
{ message: '"books[0].name" is required', path: ['books', 0, 'name'] }
]
*/
```
18 changes: 9 additions & 9 deletions docs/validation/validate-raw-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ In addition to the _instance_ `validate()` method, Structure also adds a _static
const User = attributes({
name: {
type: String,
minLength: 10
minLength: 10,
},
age: {
type: Number,
required: true
}
})(class User { });
required: true,
},
})(class User {});

// Using a raw object
const rawData = {
name: 'John'
name: 'John',
};

const { valid, errors } = User.validate(rawData);

valid; // false
errors; /*
[
{ message: '"name" length must be at least 10 characters long', path: 'name' },
{ message: '"age" is required', path: 'age' }
{ message: '"name" length must be at least 10 characters long', path: ['name'] },
{ message: '"age" is required', path: ['age'] }
]
*/

// Using a structure instance
const user = new User({
name: 'Some long name'
name: 'Some long name',
});

const validation = User.validate(user);

validation.valid; // false
validation.errors; /*
[
{ message: '"age" is required', path: 'age' }
{ message: '"age" is required', path: ['age'] }
]
*/
```

0 comments on commit 98c0de9

Please sign in to comment.