Re: inet value validation

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inet value validation
Date: 2006-04-16 16:54:20
Message-ID: 20060416165420.GB5088@KanotixBox
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

pgsql-general(at)list(dot)coretech(dot)ro <pgsql-general(at)list(dot)coretech(dot)ro> schrieb:

>
> hello,
>
> I want to write a function to validate an inet data type, but I figure that
> postgres should already have such a function to use before inserting values
> in inet type columns.
> is it possible to access postgres's validation function for inet types ? I
> have snooped through the catalogs but I can not find such a function.

You can try to cast a string into inet like this:

test=# select '127.0.0.255'::inet;
inet
-------------
127.0.0.255
(1 row)

test=# select '127.0.0.256'::inet;
ERROR: invalid input syntax for type inet: "127.0.0.256"

Now you can write a little function to do this:

,----[ a little function ]
| create or replace function check_ip(varchar) returns bool as $$
| declare
| i inet;
| begin
| i := $1::inet;
| return 't'::bool;
| EXCEPTION WHEN invalid_text_representation then
| return 'f'::bool;
| end;
| $$ language plpgsql immutable strict;
`----

You can use this function now inside a transaction.

HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknow)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message pgsql-general@list.coretech.ro 2006-04-16 17:16:18 Re: inet value validation
Previous Message Michael Fuhr 2006-04-16 16:50:26 Re: unique index on variable time