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

Regex patterns lose escape characters #378

Open
dmipeck opened this issue Jan 31, 2025 · 5 comments
Open

Regex patterns lose escape characters #378

dmipeck opened this issue Jan 31, 2025 · 5 comments

Comments

@dmipeck
Copy link

dmipeck commented Jan 31, 2025

I'm trying to generate the devContainer.base.json schema with the following:

go run github.com/atombender/go-jsonschema --package=devcontainer --output=schema.go https://raw.githubusercontent.com/devcontainers/spec/refs/heads/main/schemas/devContainer.base.schema.json

The schema contains the following spec:

"memory": {
    "type": "string",
    "pattern": "^\\d+([tgmk]b)?$",
    "description": "Amount of required RAM in bytes. Supports units tb, gb, mb and kb."
}

Which is generated as:

if plain.Memory != nil {
	if matched, _ := regexp.MatchString("^\d+([tgmk]b)?$", string(*plain.Memory)); !matched {
		return fmt.Errorf("field %s pattern match: must match %s", "^\d+([tgmk]b)?$", "Memory")
	}
}

Resulting in an "unknown escape sequence" error for \d

@RobQuistNL
Copy link
Contributor

This also causes another bug I have, the \n and \r characters (linebreak and carriage return) are parsed as-is, introducing newlines in the generating file, causing syntax errors.

@RobQuistNL
Copy link
Contributor

For me the newline issues are fixed by #295

Running the master branch works for me, what branch are you running @dmipeck ?

@RobQuistNL
Copy link
Contributor

EDIT: They are not. They are now encapsulated in backticks (`) which resolves the syntax error, but still isn't what I want:

Spec:

{
    "expressionSyntax": {
      "type": "string",
      "$comment": "escape `{` and `}` in pattern to be unicode compatible",
      "pattern": "^\\$\\{\\{(.|[\r\n])*\\}\\}$"
    }
}

Generated:

// UnmarshalJSON implements json.Unmarshaler.
func (j *ExpressionSyntax) UnmarshalJSON(b []byte) error {
	type Plain ExpressionSyntax
	var plain Plain
	if err := json.Unmarshal(b, &plain); err != nil {
		return err
	}
	if matched, _ := regexp.MatchString(`^\$\{\{(.|[
])*\}\}$`, string(plain)); !matched {
		return fmt.Errorf("field %s pattern match: must match %s", "", `^\$\{\{(.|[
])*\}\}$`)
	}
	*j = ExpressionSyntax(plain)
	return nil
}

@RobQuistNL
Copy link
Contributor

So I'm working on this now, but I think the generated output is correct; \d should be a digit character (same as [0-9]).

The error you were getting was likely due to the string being written with double quotes (") instead of backticks. Be sure to run this latest master branch (not sure if the repository was forwarded correctly, given your run URL was go run github.com/atombender/go-jsonschema instead of go run github.com/omissis/go-jsonschema

I've added a testcase, just in case, as well as fixed my newline / linefeed issue, in this PR: https://github.com/omissis/go-jsonschema/pull/380/files#diff-0f1966ad78d962abdb496073e8f7c44a8d98ed6916437fef88284a571388f6be

@dmipeck
Copy link
Author

dmipeck commented Feb 5, 2025

I was using v0.17.0 but it's working for me on main. Doesn't look like the url made any difference

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

2 participants