UNPKG

@shko.online/dataverse-odata

Version:

This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.

40 lines (39 loc) 1.13 kB
import { atMostOnce } from './validators/atMostOnce'; import { hasContent } from './validators/hasContent'; const option = '$orderby'; const edmProperty = /\w{1-255}/gi; /** * Parses the {@link ODataOrderBy.$orderby $orderby} query * @returns Returns `false` when the parse has an error */ export const getOrderByFromParser = (parser, result) => { let value = parser.getAll(option); if (value.length === 0) { return true; } if (!atMostOnce(option, value, result) || !hasContent(option, value, result)) { return false; } let $orderby = value[0].trimEnd(); const orderByArray = []; for (let i = 0; i < $orderby.length; i++) { if (false /* syntax error */) { result.error = { code: '0x0', message: `Syntax error at position ${i} in '${$orderby}'.` }; return false; } } orderByArray.forEach(orderBy => { if (!orderBy.column?.match(edmProperty)) { result.error = { code: '0x80060888', message: 'Order By Property must be of type EdmProperty' }; return false; } }); result.$orderby = orderByArray; return true; };