-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor everything - include typeError messaging as a validation err…
…or, include transforms in ast, include funky validators (negate, serial, oneOfType) in ast
- Loading branch information
1 parent
db03336
commit 875191c
Showing
38 changed files
with
466 additions
and
115 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
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,15 @@ | ||
const processor = (fn, ...args) => fn(...args); | ||
|
||
export default (property, source, err, process = processor) => ( | ||
ast, | ||
options | ||
) => { | ||
const [name, ...args] = Array.isArray(ast) ? ast : [ast]; | ||
const userDefined = options[property][name]; | ||
const fn = userDefined || source[name]; | ||
if (!fn) throw new Error(err(name)); | ||
|
||
// userDefined transforms/validators must always | ||
// be a function that accepts options and returns a transform/validator factory | ||
return process(userDefined ? fn(options) : fn, ...args); | ||
}; |
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,11 @@ | ||
import * as transforms from '../transforms'; | ||
import astToFn from './astToFn'; | ||
|
||
const processor = astToFn( | ||
'transforms', | ||
transforms, | ||
name => `No transform found for ${name}` | ||
); | ||
|
||
export const createTransforms = (ast, options) => | ||
ast.map(v => processor(v, options)); |
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
import * as validators from '../validators'; | ||
import { recursivelyConvertRefs } from './utils'; | ||
import astToFn from './astToFn'; | ||
|
||
const processor = astToFn( | ||
'validators', | ||
validators, | ||
name => `No validator found for ${name}`, | ||
(fn, ...args) => fn(...recursivelyConvertRefs(...args)) | ||
); | ||
|
||
export const createValidators = (ast, options) => | ||
ast.map(v => createValidator(v, options)); | ||
|
||
export const createValidator = (ast, options) => processor(ast, options); |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export { default as matches } from './matches'; | ||
export { createSchemas, createSchema } from './createSchema'; | ||
export { createValidators, createValidator } from './createValidator'; |
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,3 @@ | ||
// these transforms require special handling in ast-land | ||
export { default as reject } from './reject'; | ||
export { default as unique } from './unique'; |
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,4 @@ | ||
import matches from '../matches'; | ||
|
||
export default options => where => value => | ||
Array.isArray(value) ? value.filter(v => !matches(where, v, options)) : value; |
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,11 @@ | ||
import { getter } from 'property-expr'; | ||
const def = (a, b) => b === a; | ||
|
||
const createBy = using => | ||
using ? (a, b) => getter(using, true)(a) === getter(using, true)(b) : def; | ||
|
||
export default () => by => value => { | ||
if (!Array.isArray(value)) return value; | ||
const fn = createBy(by); | ||
return value.filter((a, idx, arr) => arr.findIndex(b => fn(a, b)) === idx); | ||
}; |
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,4 @@ | ||
// these validators require special handling in ast-land | ||
export { default as negate } from './negate'; | ||
export { default as oneOfType } from './oneOfType'; | ||
export { default as serial } from './serial'; |
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,4 @@ | ||
import { negate } from '../../validators'; | ||
import { createValidator } from '../createValidators'; | ||
|
||
export default options => def => negate(createValidator(def, options)); |
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,5 @@ | ||
import { oneOfType } from '../../validators'; | ||
import { createSchema } from '../createSchema'; | ||
|
||
export default options => types => | ||
oneOfType(types.map(t => createSchema(t, options))); |
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,5 @@ | ||
import { serial } from '../../validators'; | ||
import { createValidator } from '../createValidators'; | ||
|
||
export default options => defs => | ||
serial(...defs.map(d => createValidator(d, options))); |
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
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
// nullables and undefined's will never get pased to this function | ||
export default v => v.toString(); | ||
|
||
export const trim = () => v => v && v.trim(); | ||
export const strip = () => v => v && v.replace(/\s/g); | ||
export const uppercase = () => v => v && v.toUpperCase(); | ||
export const lowercase = () => v => v && v.toLowerCase(); |
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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
export default (validationError, multiple = false) => | ||
validationError | ||
? validationError.inner.reduce( | ||
(acc, { path, message }) => ({ | ||
...acc, | ||
[path]: multiple | ||
? [...(acc[path] || []), message] | ||
: acc.path | ||
? acc.path | ||
: message, | ||
}), | ||
? Object.entries( | ||
validationError.inner.reduce( | ||
(acc, { path, message }) => ({ | ||
...acc, | ||
[path]: multiple | ||
? [...(acc[path] || []), message] | ||
: acc.path | ||
? acc.path | ||
: message, | ||
}), | ||
{} | ||
) | ||
).reduce( | ||
// if we are getting errors from something with a non-inner | ||
// schema then just return the errors, otherwise return a map | ||
// of the errors by path | ||
(acc, [path, errs]) => | ||
path === 'undefined' ? errs : { ...acc, [path]: errs }, | ||
{} | ||
) | ||
: {}; |
Oops, something went wrong.