question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strip and noUnknown

See original GitHub issue

I’m trying to achieve the following.

const testSchema = yup.object().shape({
  id: yup.mixed().strip(),
  name: yup.string(),
}).noUnknown();

const schemaOptions = {
  abortEarly: false,
  strict: true,
};

test('id is stripped', t => (
  testSchema.validate(
    { id: 'test', name: 'test' },
    schemaOptions
  ).then(params => {
    t.false('id' in params);
    t.true('test' in params);
  });
));

test('unknown keys fail', t => (
  testSchema.validate(
    { unknownKey: true }
    schemaOptions,
  ).catch(() => {
    t.pass();
  });
));

I can’t seem to get the test to pass, no matter what I do for schemaOptions. Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jquensecommented, Jun 20, 2016

Ah ok so you want only the validation of noUnknown to run but not to actually strip those keys? You can’t have both right now using the built in validations. however you can do it with a custom validation fairly easily.

schema.test('no-unknown', "you can't have unknown keys", function (value) {
  let known = Object.keys(this.fields)
  let unknownKeys = Object.keys(value || {}).filter(key => known.indexOf(key) === -1);

  return value == null || unknownKeys.length === 0;
})

I’d also take PR that splits the noUnknown method into two methods noUnknown and stripUnknown for the validation and transformation respectively.

0reactions
jquensecommented, Jun 20, 2016

you can always use two schema as well, one just for casting to your desired format

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown charge from Stripe : Stripe: Help & Support
If you see “STRIPE” on your bank statement and you're unsure which business has charged you, you can use our secure charge lookup...
Read more >
How to only return the needed validated values using Yup
stripUnknown is indeed a solution. I have 2 other proposals as solution. You can use object.noUnknown. Validate that the object value only ...
Read more >
unknown argument '-strip-all' (llvm-strip.exe) - Felgo
I have using the latest Felgo and NDK version. But, when try to build for android. This error occurred:
Read more >
json-schema-strip-unknown - npm package - Snyk
Strips unknown values from a json-schema (forked from json-schema-filter to fix bugs) For more information about how to use this package see ...
Read more >
The Unknown Comic - Comic Strip Live 1989 - YouTube
The Unknown Comic in a very funny stand up gig on Comic Strip Live in the late 80's.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found