Skip to content
This repository was archived by the owner on Feb 5, 2020. It is now read-only.

Add validate Go package#3085

Merged
squat merged 1 commit intocoreos:masterfrom
kyoto:go-pkg-validate
Mar 9, 2018
Merged

Add validate Go package#3085
squat merged 1 commit intocoreos:masterfrom
kyoto:go-pkg-validate

Conversation

@kyoto
Copy link
Contributor

@kyoto kyoto commented Mar 8, 2018

Based on the frontend validators from frontend/validate.js and frontend/fields.js.

@coreosbot
Copy link

Can one of the admins verify this patch?

@kyoto kyoto force-pushed the go-pkg-validate branch from 6b3e1fc to e441854 Compare March 9, 2018 07:59
@kyoto
Copy link
Contributor Author

kyoto commented Mar 9, 2018

ok to test

if err := NonEmpty(v); err != nil {
return err
}
if ip := net.ParseIP(v); ip == nil || !strings.Contains(v, ".") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to distinguish between ipv4 and ipv6, ya?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

return err
}

if mask, err := strconv.Atoi(strings.Split(v, "/")[1]); err != nil || mask < 16 || mask > 28 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this depends implicitly on the split slice having at least two elements. we know that this should be the case since we have validated the cidr already.

Instead of splitting and parsing the string, I think it would be better to:

_, c, err := net.ParseCIDR(v)
if err != nil {
    return errors.New("not a valid CIDR")
}
if _, mask = c.Mask.Size(); mask < 16 || mask > 28 {
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return nil
}

// Host checks if the given string is either a valid v4 IP address or a valid domain name and returns an error if not.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we normally just say IPv4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if err := NonEmpty(v); err != nil {
return err
}
if i, err := strconv.Atoi(v); err != nil || i < 1 || i > 65535 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you could consider composing with IntRange

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const invalidPortMsg = "invalid port number"
const noCIDRNetmaskMsg = "must provide a CIDR netmask (eg, /24)"

type Test struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be an exported type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

t.Errorf("For IntRange(%q, %v, %v), expected %q, got %q", test.in, test.min, test.max, test.expected, err)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra newline here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@squat squat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks pretty good! I have a few small nits. Also, can you please add a BUILD.bazel file so this package can be compiled and tested, and then add the test to https://github.com/coreos/tectonic-installer/blob/master/installer/BUILD.bazel#L34
Please let me know if you need help with this

@kyoto kyoto force-pushed the go-pkg-validate branch from e441854 to d9359cb Compare March 9, 2018 14:11
@squat squat merged commit 3a7d562 into coreos:master Mar 9, 2018
@kyoto kyoto deleted the go-pkg-validate branch March 10, 2018 03:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants