Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Is it possible to use a CHECK constraint to test objects in a jsonb array?

I have the following domain:

CREATE DOMAIN foo AS JSONB
  NOT NULL
  CONSTRAINT is_valid CHECK (
    jsonb_typeof(VALUE) = 'array'
    -- AND is each element of array an obj 
    -- AND does each element of array have a key "id"
  );

I've tried several variants of ALL(), jsonb ? 'key', and array_agg(jsonb_array_elements(VALUE)) But I just can't figure out a way to get this test done.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use stored function for that:

-- drop table if exists t;
-- drop function if exists f(jsonb);

create function f(jsonb) returns bool language plpgsql immutable as $$
begin
  if jsonb_typeof($1) <> 'array' then
    return false;
  else
    return (
      select bool_and(jsonb_typeof(j) = 'object' and j ? 'id')
      from jsonb_array_elements($1) as a(j));
  end if;
end $$;

-- test the function
select f('[{"id":1}]'::jsonb), f('{"id":1}'::jsonb), f('[{"id":1},"id"]'::jsonb);

create table t(x jsonb check (f(x)));
insert into t values('[{"id":1}]'); -- success
insert into t values('{"id":1}'); -- fail
insert into t values('[{"id":1},"id"]'); -- fail too
over 4 years ago · Santiago Trujillo Relatório

0

t=# create or replace function s89(_v jsonb) returns boolean as $$
declare
 _r boolean;
 _c int;
 _t text;
begin
  with b as (
    with t as (
      select _v "value"
    )
    select
      jsonb_array_elements(value)->>'id' id
    , jsonb_array_length(value) c
    from t
    )
    select array_length(array_agg(id),1) = c,c,array_agg(id)::text into _r, _c,_t
    from b
    where id is not null
    group by c;
    raise info '%',_c||_t;
  return _r;
end;
$$ language plpgsql
;
CREATE FUNCTION
t=# select s89('[{"id":3},{"id":4},4]'::jsonb);
INFO:  3{3,4}
 s89
-----
 f
(1 row)

t=# select s89('[{"id":3},{"idr":4}]'::jsonb);
INFO:  2{3}
 s89
-----
 f
(1 row)

t=# select s89('[{"id":3},{"id":4}]'::jsonb);
INFO:  2{3,4}
 s89
-----
 t
(1 row)

t=# CREATE DOMAIN foo AS JSONB
  NOT NULL
  CONSTRAINT is_valid CHECK (
    jsonb_typeof(VALUE) = 'array' and s89(VALUE)
  );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda