Git Product home page Git Product logo

Comments (3)

gcanti avatar gcanti commented on June 30, 2024 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.

SeeThruHead avatar SeeThruHead commented on June 30, 2024

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.

ferndopolis avatar ferndopolis commented on June 30, 2024

👍 this was helpful and might want to add to the readme
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.