Comments (3)
Hi @SeeThruHead,
In case of union of structs you must implement a dispatch
function (https://github.com/gcanti/tcomb/blob/master/docs/API.md#the-dispatch-function), since in general tcomb
doesn't know which constructor to use:
const data = {
text: 'whyoff'
}
const Text = t.struct({ text: t.String }, 'Text')
const Url = t.struct({ url: t.String }, 'Url')
const Attributes = t.union([
Text,
Url
], 'Attributes')
// dummy dispatch: given an input the dispatch function should return the suitable constructor
Attributes.dispatch = function (x) {
return x.text ? Text : Url
}
const result = t.validate(data, Attributes)
console.log(result)
// => Struct {errors: Array[0], value: Object} // OK!
from tcomb-validation.
Oh I see, I had thought that validate would run through until it found one that matched. Thanks again!
--
Shane Keulen
Sent with Airmail
On March 14, 2016 at 1:50:31 PM, Giulio Canti ([email protected]) wrote:
const data = {
text: 'whyoff'
}
const Text = t.struct({ text: t.String }, 'Text')
const Url = t.struct({ url: t.String }, 'Url')
const Attributes = t.union([
Text,
Url
], 'Attributes')
// dummy dispatch: given an input the dispatch function should return the suitable constructor
Attributes.dispatch = function (x) {
return x.text ? Text : Url
}
const result = t.validate(data, Attributes)
console.log(result)
from tcomb-validation.
today i ran into a problem where the variable which is initalize as null and must be a number between a range, but am using custom error messages. here's the code
const mysubtype = (type, getValidationErrorMessage, name) => {
let Subtype = t.subtype(type, (x) => {
return !t.String.is(getValidationErrorMessage(x));
}, name);
Subtype.getValidationErrorMessage = getValidationErrorMessage;
return Subtype;
}
const notNull = mysubtype(t.Nil, (s) => {
if (!s) return 'Required';
});
const cap = mysubtype(t.Number, (s) => {
if (!s) return 'Required';
if (s <= 1) return 'Must be greater than one';
if (s > 25) return 'Cannot be greater than 25';
});
export const Capacity = t.union([notNull, cap], 'Capacity');
Capacity.dispatch = (x) => { return x ? cap : notNull };
from tcomb-validation.
Related Issues (20)
- Maybe value converted from undefined to null after validation? HOT 2
- Async validation support ? HOT 2
- allow casting schema HOT 3
- subtype documentation missing HOT 3
- custom validation messages does not work when value is undefined. HOT 2
- Struct with predicate
- [rfc] ValidationError: relax message type HOT 1
- Testing list of functions?
- `tcomb-validation` error message doesn't contain struct name HOT 4
- Create refinement with two parameter passed HOT 2
- Question about the RegExp type and how it's used.
- Start reading flow Types from *.js.flow files.
- Having trouble using Date validation HOT 5
- Question about usage and need HOT 3
- http post validation HOT 3
- http post validation (Number Params Problem) HOT 1
- How to modify struct value
- Don't run check again for error message
- [question] Extra value in error.paths array HOT 2
- Unexpected behavior of intersections in strict mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tcomb-validation.