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

Unable to morph process.env values to non-strings #1032

Closed
TizzySaurus opened this issue Jul 2, 2024 · 4 comments
Closed

Unable to morph process.env values to non-strings #1032

TizzySaurus opened this issue Jul 2, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@TizzySaurus
Copy link
Contributor

Report a bug

πŸ”Ž Search Terms

pipe, object, process.env

🧩 Context

  • ArkType version: 2.0-dev.26
  • TypeScript version (5.1+): 5.5
  • Other context you think may be relevant (JS flavor, OS, etc.): N/A

πŸ§‘β€πŸ’» Repro

import { type } from "arktype"

process.env.VALUE = "true"

const t = type({
    VALUE: ["'true'|'false'", "=>", x => {console.log("hi from morph"); return x === "true"}]
})
const envs1 = t.assert(process.env) // runs morph but doesn't change `VALUE` to boolean
console.log(typeof envs1.VALUE) // string

const envs2 = t.assert(Object.fromEntries(Object.entries(process.env))) // runs morph and changes `VALUE` to boolean
console.log(typeof envs2.VALUE) // boolean
@TizzySaurus TizzySaurus added the bug Something isn't working label Jul 2, 2024
@TizzySaurus
Copy link
Contributor Author

TizzySaurus commented Jul 7, 2024

After further investigation with @dikatok on Discord, it seems the cause of this is actually that process.env is an "exotic object" (i.e. "not a standard object"), for which in this case it's impossible to have a non-string value (values get coerced to string):
image

More info about this process.env functionality is detailed within nodejs/node#12126.

It would be nice if ArkType could somehow detect this, and automatically convert to a standard object via the spread operator ({...process.env, keyBeingMorphed: resultOfMorph}). The following changes to this code seem to do the trick, but I've not thoroughly tested:

				// apply the morph function and assign the result to the
				// corresponding property, or to root if path is empty
				if (parent === undefined) this.root = result
				else parent[key!] = result

                                /* Changes are all below */
				// `process.env` doesn't allow non-string values,
				// so we have to create a copy object.
                                // https://github.com/nodejs/node/issues/12126
				if (typeof result !== "string" && typeof parent[key!] === "string")
					parent = {...parent, key: result}

@TizzySaurus TizzySaurus changed the title Pipe doesn't always correctly apply to objects Unable to morph process.env values to non-strings Jul 7, 2024
@Dimava
Copy link
Contributor

Dimava commented Jul 8, 2024

This should be a clone-morph, do we have those?

I'm not sure if AT should have exceptions for exotic objects (process.env, localStorage, Proxy instances)

@dikatok
Copy link

dikatok commented Jul 8, 2024

@Dimava the best workaround for those kind of objects is cloning by ourselves on application level before feeding it to AT? at least for the moment?

@ssalbdivad
Copy link
Member

@dikatok Yes. I do plan eventually to offer a config option to clone before morphing:

#959

For now, just spread process .env before applying your morph

@ssalbdivad ssalbdivad closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done (merged or closed)
Development

No branches or pull requests

4 participants