# Validating the content of a struct field

**URL:** <https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717>\
**Category:** New to Julia\
**Tags:** struct, constructors\
**Created:** [February 16, 2023, 10:59am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717 "2023-02-16T10:59:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sibbe\_Bakker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sibbe_bakker/32/46951_2.png) [@Sibbe\_Bakker](https://discourse.julialang.org/u/Sibbe_Bakker)\
**Post date:** [February 16, 2023, 10:59am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/1 "2023-02-16T10:59:45Z")

</div>

Hello all, I am a beginner in the julia language and I quite like it.

I am trying to make a struct that checks whether the input is valid:

```julia
struct Dna
    # Basic elements of a DNA string
    sequence::String
    identifier::String
    comment::String
    # Checking Wether the DNA is valid.
    Dna(sequence::String, identifier::String, comment::String) =
        validate_dna(sequence) ? new(sequence, identifier, comment) :
        throw(ArgumentError("Sequence of $identifier is invalid."))
end

function validate_dna(string::String)
    #=Validating DNA input

    :param string: String The string to be validated. 
    :return: Bool: 1 If the string is a valid DNA string, 0 otherwise.
    =#
    accepted_letters = Set(['A', 'T', 'C', 'G'])
    accepted_chars = map(x -> issubset(x, accepted_letters), collect(string))
    return all(accepted_chars)
end

```

When I execute this function; the input is not validated as I would expect:

```julia
# Expected ERROR: Sequence of 12 is invalid:
julia> a = dna("ATxCTG", "12", "a test sequence")
dna("ATxCTG", "12", "a test sequence")
# Expected no error
julia> b = dna("ATCTG", "12", "a test sequence")
dna("ATCTG", "12", "a test sequence")

```

No error messages are generated. Could somebody help me improve this code?

The `validate_dna()` function works

```julia
julia> validate_dna("AAx")
false
julia> validate_dna("AAG")
true

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 16, 2023, 11:04am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/2 "2023-02-16T11:04:31Z")

</div>

Have you checked your capitalization?

---

<div class="post-metadata">

**Author:** ![Sibbe\_Bakker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sibbe_bakker/32/46951_2.png) [@Sibbe\_Bakker](https://discourse.julialang.org/u/Sibbe_Bakker)\
**Post date:** [February 16, 2023, 11:10am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/3 "2023-02-16T11:10:55Z")

</div>

Thank you for the response, what capitalisation do you mean? I have checked the spelling of the variables, I think they are all correctly spelled. If you think I made a mistake could you point out where it is?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 16, 2023, 11:11am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/4 "2023-02-16T11:11:56Z")

</div>

Capitalization of `Dna` (versus `dna`)

---

<div class="post-metadata">

**Author:** ![Sibbe\_Bakker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sibbe_bakker/32/46951_2.png) [@Sibbe\_Bakker](https://discourse.julialang.org/u/Sibbe_Bakker)\
**Post date:** [February 16, 2023, 11:13am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/5 "2023-02-16T11:13:50Z")

</div>

Thank you, that was quite in-attentive of me.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 16, 2023, 11:14am UTC](https://discourse.julialang.org/t/validating-the-content-of-a-struct-field/94717/6 "2023-02-16T11:14:12Z")

</div>

Just needed a fresh pair of eyes, it happens to the best of us 😉
