-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9759836
commit c58e413
Showing
8 changed files
with
238 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import struct from '..' | ||
|
||
// Define a struct to validate with. | ||
const validate = struct({ | ||
id: 'number', | ||
name: 'string', | ||
email: 'string', | ||
age: 'number', | ||
departments: ['string'], | ||
is_admin: 'boolean?', | ||
}) | ||
|
||
// Define data to be validated. | ||
const data = { | ||
id: 1, | ||
name: 'Jane Smith', | ||
email: 'jane@example.com', | ||
age: 42, | ||
departments: ['engineering', 'product'], | ||
} | ||
|
||
// Validate the data by calling `validate`. | ||
// In this case, the data is valid, so it will not throw. | ||
try { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ianstormtaylor
Author
Owner
|
||
validate(data) | ||
console.log('Valid!') | ||
} catch (e) { | ||
throw e | ||
} | ||
|
||
// 'Valid!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import struct from '..' | ||
|
||
// Define a struct to validate with. | ||
const validate = struct({ | ||
id: 'number', | ||
name: 'string', | ||
email: 'string', | ||
}) | ||
|
||
// Define data to be validated. | ||
const data = { | ||
id: 1, | ||
name: true, | ||
email: 'jane@example.com', | ||
} | ||
|
||
// Validate the data by calling `validate`. In this case the | ||
// `name` property is invalid, so an error will be thrown that | ||
// you can catch and customize to your needs. | ||
try { | ||
validate(data) | ||
console.log('Valid!') | ||
} catch (e) { | ||
switch (e.code) { | ||
case 'property_invalid': { | ||
const err = new Error(`user_${e.key}_invalid`) | ||
err.attribute = e.key | ||
err.value = e.value | ||
throw err | ||
} | ||
case 'property_required': { | ||
const err = new Error(`user_${e.key}_required`) | ||
err.attribute = e.key | ||
throw err | ||
} | ||
case 'property_unknown': { | ||
const err = new Error(`user_attribute_unknown`) | ||
err.attribute = e.key | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
// Error: 'user_name_invalid' { | ||
// attribute: 'name', | ||
// value: false, | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { superstruct } from '..' | ||
import isEmail from 'is-email' | ||
import isUuid from 'is-uuid' | ||
import isUrl from 'is-url' | ||
|
||
// Define a `struct` with custom types. | ||
const struct = superstruct({ | ||
types: { | ||
uuid: v => isUuid.v4(v), | ||
email: v => isEmail(v) && v.length < 256, | ||
url: v => isUrl(v) && v.length < 2048, | ||
} | ||
}) | ||
|
||
// Define a struct which returns a `validate` function. | ||
const validate = struct({ | ||
id: 'uuid', | ||
name: 'string', | ||
email: 'email', | ||
website: 'url?', | ||
}) | ||
|
||
// Define data to be validated. | ||
const data = { | ||
id: 'c8d63140-a1f7-45e0-bfc6-df72973fea86', | ||
name: 'Jane Smith', | ||
email: 'jane@example.com', | ||
website: 'https://jane.example.com', | ||
} | ||
|
||
// Validate the data by calling `validate`. | ||
// In this case, the data is valid, so it will not throw. | ||
try { | ||
validate(data) | ||
console.log('Valid!') | ||
} catch (e) { | ||
throw e | ||
} | ||
|
||
// 'Valid!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import struct from '..' | ||
|
||
// Define an auto-incrementing unique id. | ||
let uid = 1 | ||
|
||
// Define a struct to validate with. | ||
const validate = struct({ | ||
id: 'number', | ||
name: 'string', | ||
email: 'string', | ||
age: 'number', | ||
is_admin: 'boolean?', | ||
created_at: 'date', | ||
}, { | ||
id: () => uid++, | ||
is_admin: false, | ||
age: 0, | ||
created_at: () => new Date(), | ||
}) | ||
|
||
// Define data to be validated. | ||
const data = { | ||
name: 'Jane Smith', | ||
email: 'jane@example.com', | ||
age: 42, | ||
} | ||
|
||
// Validate the data by calling `validate`, and storing the | ||
// return value in the `user` variable. Any property that | ||
// wasn't defined will be set to its default value. | ||
let user | ||
|
||
try { | ||
user = validate(data) | ||
console.log('Valid!', user) | ||
} catch (e) { | ||
throw e | ||
} | ||
|
||
// 'Valid!', { | ||
// id: 0, | ||
// name: 'Jane Smith', | ||
// email: 'jan@example.com', | ||
// age: 42, | ||
// is_admin: false, | ||
// created_at: Date, | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import struct from '..' | ||
|
||
// Define a struct to validate with. | ||
const validate = struct({ | ||
id: 'number', | ||
name: 'string', | ||
email: 'string', | ||
}) | ||
|
||
// Define data to be validated. | ||
const data = { | ||
id: 1, | ||
name: true, | ||
email: 'jane@example.com', | ||
} | ||
|
||
// Validate the data by calling `validate`. In this case the | ||
// `name` property is invalid, so a `property_invalid` error | ||
// will be thrown. | ||
try { | ||
validate(data) | ||
} catch (e) { | ||
throw e | ||
} | ||
|
||
// StructError: 'Expected the `name` property to be of type "string", but it was `false`.' { | ||
// code: 'property_invalid', | ||
// type: 'string', | ||
// path: ['name'], | ||
// key: 'name', | ||
// value: false, | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why the redundant try/catch blocks in this and other files?