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.

*Question* Is it possible to apply "strict" mode to avoid coercing data types, but still allowing transforming?

See original GitHub issue

I have an API that receives different fields, one of which would be a zipcode-like document. Normally, users can send documents with punctuation, like dashes and commas, and I’d like to:

  1. Not allow any values that aren’t strings (example: Yup coerces [1,2] into 12 in non strict mode)
  2. Remove non digits from the text (I used transform() to change 1.2.3 into 123, for example).

However, it seems strict mode only allows one or the other, never both. I can think of solutions involving the test() method, but just to make sure I’m not missing out on any features, is it feasible without using test()?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jquensecommented, Jun 10, 2020

There isn’t a built in way, but you can build your own string schema that does it.

class StrictString extends string {
  constructor() {
    super()
    this.transforms = [] // remove the default coercion transforms
  }
}
0reactions
ProtoCh4oscommented, Jun 17, 2020

Great! It worked! Thanks for the help @jquense

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strict mode - JavaScript - MDN Web Docs
JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode".
Read more >
about "strict" mode - SQLite Forum
A strict setting is stored in each database file, and is set like PRAGMA page_size . To convert an existing database to strict...
Read more >
Reasons for choosing not to use strict mode? : r/PHP - Reddit
Avoiding strict types in PHP because coercion at language runtime boundaries where type metadata is implicitly missing is a pretty weak reason, ...
Read more >
What is "strict mode" and how is it used? - Stack Overflow
Strict mode is a new feature in ECMAScript 5 that allows Developers to put the code inside the "strict" context. This strict context...
Read more >
Strict mode - Ajv JSON schema validator
Strict mode intends to prevent any unexpected behaviours or silently ignored mistakes in user schemas. It does not change any validation results compared ......
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found