-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isShape validator and better documentation generation #183
base: master
Are you sure you want to change the base?
Changes from all commits
1f8fadc
17e63ae
8439be6
6e1e7e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Writes information about if a type is required and if it can be empty inline | ||
*/ | ||
export default function writeInfoInline(type, canBeEmpty, required) { | ||
const empty = canBeEmpty ? '?' : ''; | ||
return type && `${required ? `<${empty}${type}>` : `[${empty}${type}]`}`; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { isPlainObject, difference } from 'lodash'; | ||
|
||
import createInfoObject from '../helpers/createInfoObject'; | ||
import getSuggestions from '../../helpers/getSuggestions'; | ||
import isValid from '../helpers/isValid'; | ||
import toObject from '../../converters/toObject'; | ||
import writeInfoInline from '../helpers/writeInfoInline'; | ||
|
||
export default function isShape(shape, { strict = true } = {}) { | ||
if (!shape || !isPlainObject(shape) || Object.keys(shape).length === 0) { | ||
throw new Error('The isShape validator requires that a shape object is defined.'); | ||
} | ||
|
||
return (input, info) => { | ||
const keys = Object.keys(shape); | ||
|
||
if (info) { | ||
const types = Object.keys(shape).map((key) => createInfoObject({ | ||
validator: shape[key], | ||
wrapper: (...args) => `${key}: ${writeInfoInline(...args)}`, | ||
}).type).join(', '); | ||
|
||
// We do not need a speical converter, since there will never be the case that we | ||
// have a input that is not on the ordinary object form, meaning that we will accept | ||
// whatever we get back from it. | ||
// In addtion to this we should not use this validator for something we get on the | ||
// command line since we have better ways to manage shape like object there. | ||
return createInfoObject({ | ||
validator: types, | ||
converter: () => toObject, | ||
wrapper: (wrap) => `{ ${wrap} }`, | ||
canBeEmpty: true, | ||
}); | ||
} | ||
|
||
if (input === undefined || input === null) { | ||
return true; | ||
} | ||
|
||
if (!isPlainObject(input)) { | ||
return 'Was not an object and can therefore not have a shape!'; | ||
} | ||
|
||
|
||
for (const key of keys) { | ||
const result = isValid(input[key], shape[key]); | ||
|
||
if (result !== true) { | ||
if (isPlainObject(result)) { | ||
return { | ||
key: `${key}.${result.key}`, | ||
value: result.value, | ||
message: result.message, | ||
}; | ||
} | ||
return { | ||
key: `${key}`, | ||
value: input[key], | ||
message: result, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would there be any way to create a composed validation error of all properties that failed to validate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to both fail early and to not overwhelm the user with error messages. |
||
} | ||
} | ||
|
||
if (strict) { | ||
const diff = difference(Object.keys(input), keys); | ||
|
||
if (diff.length > 0) { | ||
return `Unknown propertys where found, make sure this is correct.\n${getSuggestions(diff, keys)}`; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to force this to always return a string? Right now it'll return a falsy value or a string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that
type
will only ever be the empty string, and because of that we will return the empty string. Could be discussed if it would be better to not have such implicit assumptions.