Skip to content
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

$transform with array cast #13

Closed
vkarpov15 opened this issue Oct 28, 2017 · 0 comments
Closed

$transform with array cast #13

vkarpov15 opened this issue Oct 28, 2017 · 0 comments

Comments

@vkarpov15
Copy link
Contributor

const NameType = new Archetype({
  first: { $type: 'string' },
  last: { $type: 'string' }
}).compile('NameType');

const Params = new Archetype({
  names: { 
    $type: [{ $type: NameType, $transform: JSON.parse }],
    $default: []
  }
}).compile('Params');

app.get('/', function(req, res) {
  const { names } = new Params(req.query);
  res.json(names.map(n => `${n.first} ${n.last}`).join(','));
});

run().catch(error => console.error(error));

async function run() {
  await app.listen(3000);

  const root = 'http://localhost:3000';
  const names = [
    { first: 'James', last: 'Kirk' },
    { first: 'Leonard', last: 'McCoy' }
  ];
  console.log(await superagent.get(`${root}`).then(res => res.text));
  console.log(await superagent.get(`${root}?names=${JSON.stringify(names[0])}`).then(res => res.text));
  let url = `${root}?names=${JSON.stringify(names[0])}&names=${JSON.stringify(names[1])}`;
  console.log(await superagent.get(url).then(res => res.text));
}
vkarpov15 added a commit that referenced this issue Nov 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant