attribute with null or regexp validated values

4 views
Skip to first unread message

andrea.s...@gmail.com

unread,
Oct 3, 2018, 6:35:04 AM10/3/18
to praxis-support
Hi,

is there a (correct) way to indicate on a resource that a value for a given attribute can be:

- null
- a not null value validated by a regexp

i.e:

Given this optional attribute:

attribute :cf, String, regexp: /^[A-Z]{6}[0-9L-NP-V]{2}[A-EHLMPR-T][0-9L-NP-V]{2}[A-Z][0-9L-NP-V]{3}[A-Z]$/

on update (a PUT request with a body containing a resource with this :cf attribute) I want to be able to accept both:

{ ... cf: 'PPPPLT80R10M082K', ... }

which means *set attribute cf to this new value (PPPPLT80R10M082K)*, but also:

{ ... cf: null ... }

which means *clear the cf value*.

By removing the regexp specification, I'm able to accept null, but I loose strict validation on the field when it is given with other values than `null`.

Is there an "official" way to achieve this behaviour?

Thank you,
--
Andrea Salicetti

Josep Blanquer

unread,
Oct 5, 2018, 2:08:07 AM10/5/18
to Salicetti Andrea, praxis-support
Andrea,

 Yes, that's totally possible, and in fact, this is how it is designed. I am not sure how you have set it up to get the behavior you're describing.

I created a very basic example from an empty generated app.

Here's the design:
module Endpoints
class Tests
include Praxis::ResourceDefinition

action :update do
description 'Update'
routing { patch '/:id' }
params { attribute :id, Integer }
payload do
attribute :name, String, regexp: /[0-9]+/
attribute :phone, String
end
response :no_content
end
end
end

Here's the Controller:
module Controllers
class Tests
include Praxis::Controller
implements Endpoints::Tests

def update(id:)
puts "Updating #{id} with payload: #{request.payload.dump}"
Praxis::Responses::NoContent.new
end
end
end

And here's the behavior: 

passing an invalid regex fails:

$ curl -i -X PATCH 'http://localhost:9292/tests/123' -H 'Content-Type: application/json' -d '{"name":"bad"}'
HTTP/1.1 400 Bad Request
Content-Type: application/json
Transfer-Encoding: chunked
Server: WEBrick/1.3.1 (Ruby/2.4.3/2017-12-14)
Date: Fri, 05 Oct 2018 06:02:52 GMT
Connection: Keep-Alive

{
  "name": "ValidationError",
  "summary": "Errors validating payload data",
  "errors": [
    "$.payload.name value (bad) does not match regexp (/[0-9]+/)"
  ]
}

passing a good one succeeds:

$ curl -i -X PATCH 'http://localhost:9292/tests/123' -H 'Content-Type: application/json' -d '{"name":"1234"}'
HTTP/1.1 204 No Content
Server: WEBrick/1.3.1 (Ruby/2.4.3/2017-12-14)
Date: Fri, 05 Oct 2018 06:03:47 GMT
Connection: Keep-Alive

not passing the attribute at all succeeds (same as passing it with null value):

$ curl -i -X PATCH 'http://localhost:9292/tests/123' -H 'Content-Type: application/json' -d '{}'
HTTP/1.1 204 No Content
Server: WEBrick/1.3.1 (Ruby/2.4.3/2017-12-14)
Date: Fri, 05 Oct 2018 06:04:07 GMT
Connection: Keep-Alive

...can you see what the differences in your code are as to find out what it is behaving different than this?

Cheers,

Josep M.

--
You received this message because you are subscribed to the Google Groups "praxis-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to praxis-suppor...@googlegroups.com.
To post to this group, send email to praxis-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/praxis-support/5dfb1d0b-206d-42e1-8eaa-c9a3a897060f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

andrea.s...@gmail.com

unread,
Oct 5, 2018, 4:47:07 AM10/5/18
to praxis-support
You're perfectly right: i double check a call with null value and it works.

The null value seems always accepted, even when validations are present.

Thank you,
--
AS
Reply all
Reply to author
Forward
0 new messages