Skip to main content

validate boolean expressions with XPath syntax

Project description

https://travis-ci.org/znc-sistemas/xpath_validator.svg?branch=master https://coveralls.io/repos/github/znc-sistemas/xpath_validator/badge.svg?branch=master Package license

Validate boolean expressions with XPath syntax

validate(‘expression’, ‘value’, ‘context’, ‘returns_bool’)

  • expression: string with the expression text

  • value: value that will be validated (it is replaced by the occurrences of ‘.’ in the expression)

  • context: dictionary that will make substitutions in $ {name}

  • returns_bool: if true, automatically returns a boolean

Examples

>>> validate('. >= 1 and . <= 100', 10)
True
>>> validate('. >= 1 and . <= 100', 101)
False
>>> validate('not(. >= 1 and . <= 100)', -10)
True
>>> validate('ceiling(.) = 5', 4.3)
True
>>> validate('floor(.) = 4', 4.7)
True
>>> validate('floor(.) = 4', 4.2)
True
>>> validate('int(.) = 4', 4.7)
True
>>> validate('number(.) = 3.2', 3.2)
True
>>> validate('choose(true(), 1, 2) = 1', None)
True
>>> validate('choose(false(), 1, 2) = 2', None)
True
>>> validate('contains("abc", .)', "b")
True
>>> validate('contains("abc", .)', "e")
False
>>> validate('number(string(.)) = 6', 6)
True
>>> validate('string-length(.) = 15', "632587415222360")
True
>>> validate('string_length(.) = 11', "40258997853")
True
>>> validate('string_length(.) = 7', "abacate")
True
>>> validate('5 < .', 10)
True
>>> validate('5 > .', 10)
False
>>> validate('5 != .', 5)
False
>>> validate('5 != .', 10)
True
>>> validate('5 + 5 = .', 10)
True
>>> validate('5 - 51 = .', -46)
True
>>> validate('true() or false()', None)
True
>>> validate('false() or false()', None)
False
>>> validate('(. div 5) = 2', 10)
True
>>> validate('(. * 5) = 50', 10)
True
>>> validate('(. div 5) < .', 10)
True
>>> validate('(. * 5) > .', 10)
True
>>> validate('(. mod 2) = 0', 10)
True
>>> validate('(. mod 2) = 1', 11)
True
>>> validate('(. mod 2) = 1', 10)
False
>>> validate('(. mod 2) = 0', 11)
False
>>> validate("int(format-date-time(., '%H')) = 19", '2019-05-14T19:13:35.450686Z')
True
>>> validate("int(format-date-time(., '%m')) = 5", '2019-05-14T19:13:35.450686Z')
True
>>> validate("int(format-date-time(., '%M')) = 13", '2019-05-14T19:13:35.450686Z')
True
>>> validate("int(format-date-time(., '%Y')) = 2019", '2019-05-14T19:13:35.450686Z')
True
>>> validate("int(format-date-time(., '%d')) = 14", '2019-05-14T19:13:35.450686Z')
True
>>> validate("format-date-time(., '%d/%m/%Y') = '14/05/2019'", '2019-05-14T19:13:35.450686Z')
True
>>> validate('. >= ${min} and . <= ${max}', 10, {"max": 100, "min": 10})
True
>>> validate('. >= ${min} and . <= ${max}', 10, {"max": 100, "min": 20})
False
>>> validate('${min} = "" and ${max} = ""', None, {"max": 100, "min": 20})
False
>>> validate("substring-after('aa&bb', ${sep}) = 'bb'", "&", {'sep': '&'})
True
>>> validate("substring-before('aa&bb', ${sep}) = 'aa'", "&", {'sep': '&'})
True
>>> validate("normalize-space('    abacate ') = 'abacate'", None)
True
>>> validate("starts-with('abacate', 'ab')", None)
True
>>> validate("starts-with('abacate', 'ac')", None)
False
>>> validate("uuid()", None, returns_bool=False)
'2327c8bc-ac46-4968-a73c-5f21f9e9b1ce'
>>> validate('(. div -5)', 10, returns_bool=False)
-2.0
>>> validate("selected('peixe abacate', .)", 'peixe')
True
>>> validate("selected('peixe abacate', 'peixe')", None)
True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xpath_validator-1.5.1.tar.gz (9.3 kB view details)

Uploaded Source

File details

Details for the file xpath_validator-1.5.1.tar.gz.

File metadata

  • Download URL: xpath_validator-1.5.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

File hashes

Hashes for xpath_validator-1.5.1.tar.gz
Algorithm Hash digest
SHA256 4f9958794da99e7ce3f41b51c9fed2ba7ea372fbde630641f6794055aacafc33
MD5 629123fefccd4155a88ba39d2c9f1aeb
BLAKE2b-256 b9e7fc7ad2a40d51a43748ad60d049b479c420f21133a669f70ec6fac93b996e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page